sifive_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Axiado Corporation
  5. * All rights reserved.
  6. *
  7. * This software was developed in part by Philip Paeps and Kristof Provost
  8. * under contract for Axiado Corporation.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/bus.h>
  34. #include <sys/kernel.h>
  35. #include <sys/lock.h>
  36. #include <sys/module.h>
  37. #include <sys/mutex.h>
  38. #include <sys/rman.h>
  39. #include <machine/bus.h>
  40. #include <machine/cpu.h>
  41. #include <dev/clk/clk.h>
  42. #include <dev/ofw/ofw_bus.h>
  43. #include <dev/ofw/ofw_bus_subr.h>
  44. #include <dev/ofw/openfirm.h>
  45. #include <dev/spibus/spi.h>
  46. #include <dev/spibus/spibusvar.h>
  47. #include "spibus_if.h"
  48. #if 1
  49. #define DBGPRINT(dev, fmt, args...) \
  50. device_printf(dev, "%s: " fmt "\n", __func__, ## args)
  51. #else
  52. #define DBGPRINT(dev, fmt, args...)
  53. #endif
  54. static struct resource_spec sfspi_spec[] = {
  55. { SYS_RES_MEMORY, 0, RF_ACTIVE },
  56. RESOURCE_SPEC_END
  57. };
  58. struct sfspi_softc {
  59. device_t dev;
  60. device_t parent;
  61. struct mtx mtx;
  62. struct resource *res;
  63. bus_space_tag_t bst;
  64. bus_space_handle_t bsh;
  65. void *ih;
  66. clk_t clk;
  67. uint64_t freq;
  68. uint32_t cs_max;
  69. };
  70. #define SFSPI_LOCK(sc) mtx_lock(&(sc)->mtx)
  71. #define SFSPI_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
  72. #define SFSPI_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED);
  73. #define SFSPI_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED);
  74. /*
  75. * Register offsets.
  76. * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 101.
  77. */
  78. #define SFSPI_REG_SCKDIV 0x00 /* Serial clock divisor */
  79. #define SFSPI_REG_SCKMODE 0x04 /* Serial clock mode */
  80. #define SFSPI_REG_CSID 0x10 /* Chip select ID */
  81. #define SFSPI_REG_CSDEF 0x14 /* Chip select default */
  82. #define SFSPI_REG_CSMODE 0x18 /* Chip select mode */
  83. #define SFSPI_REG_DELAY0 0x28 /* Delay control 0 */
  84. #define SFSPI_REG_DELAY1 0x2C /* Delay control 1 */
  85. #define SFSPI_REG_FMT 0x40 /* Frame format */
  86. #define SFSPI_REG_TXDATA 0x48 /* Tx FIFO data */
  87. #define SFSPI_REG_RXDATA 0x4C /* Rx FIFO data */
  88. #define SFSPI_REG_TXMARK 0x50 /* Tx FIFO watermark */
  89. #define SFSPI_REG_RXMARK 0x54 /* Rx FIFO watermark */
  90. #define SFSPI_REG_FCTRL 0x60 /* SPI flash interface control* */
  91. #define SFSPI_REG_FFMT 0x64 /* SPI flash instruction format* */
  92. #define SFSPI_REG_IE 0x70 /* SPI interrupt enable */
  93. #define SFSPI_REG_IP 0x74 /* SPI interrupt pending */
  94. #define SFSPI_SCKDIV_MASK 0xfff
  95. #define SFSPI_CSDEF_ALL ((1 << sc->cs_max)-1)
  96. #define SFSPI_CSMODE_AUTO 0x0U
  97. #define SFSPI_CSMODE_HOLD 0x2U
  98. #define SFSPI_CSMODE_OFF 0x3U
  99. #define SFSPI_TXDATA_DATA_MASK 0xff
  100. #define SFSPI_TXDATA_FULL (1 << 31)
  101. #define SFSPI_RXDATA_DATA_MASK 0xff
  102. #define SFSPI_RXDATA_EMPTY (1 << 31)
  103. #define SFSPI_SCKMODE_PHA (1 << 0)
  104. #define SFSPI_SCKMODE_POL (1 << 1)
  105. #define SFSPI_FMT_PROTO_SINGLE 0x0U
  106. #define SFSPI_FMT_PROTO_DUAL 0x1U
  107. #define SFSPI_FMT_PROTO_QUAD 0x2U
  108. #define SFSPI_FMT_PROTO_MASK 0x3U
  109. #define SFSPI_FMT_ENDIAN (1 << 2)
  110. #define SFSPI_FMT_DIR (1 << 3)
  111. #define SFSPI_FMT_LEN(x) ((uint32_t)(x) << 16)
  112. #define SFSPI_FMT_LEN_MASK (0xfU << 16)
  113. #define SFSPI_FIFO_DEPTH 8
  114. #define SFSPI_READ(_sc, _reg) \
  115. bus_space_read_4((_sc)->bst, (_sc)->bsh, (_reg))
  116. #define SFSPI_WRITE(_sc, _reg, _val) \
  117. bus_space_write_4((_sc)->bst, (_sc)->bsh, (_reg), (_val))
  118. static void
  119. sfspi_tx(struct sfspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
  120. {
  121. uint32_t val;
  122. uint8_t *p, *end;
  123. KASSERT(buf != NULL, ("TX buffer cannot be NULL"));
  124. end = buf + bufsiz;
  125. for (p = buf; p < end; p++) {
  126. do {
  127. val = SFSPI_READ(sc, SFSPI_REG_TXDATA);
  128. } while (val & SFSPI_TXDATA_FULL);
  129. val = *p;
  130. SFSPI_WRITE(sc, SFSPI_REG_TXDATA, val);
  131. }
  132. }
  133. static void
  134. sfspi_rx(struct sfspi_softc *sc, uint8_t *buf, uint32_t bufsiz)
  135. {
  136. uint32_t val;
  137. uint8_t *p, *end;
  138. KASSERT(buf != NULL, ("RX buffer cannot be NULL"));
  139. KASSERT(bufsiz <= SFSPI_FIFO_DEPTH,
  140. ("Cannot receive more than %d bytes at a time\n",
  141. SFSPI_FIFO_DEPTH));
  142. end = buf + bufsiz;
  143. for (p = buf; p < end; p++) {
  144. do {
  145. val = SFSPI_READ(sc, SFSPI_REG_RXDATA);
  146. } while (val & SFSPI_RXDATA_EMPTY);
  147. *p = val & SFSPI_RXDATA_DATA_MASK;
  148. };
  149. }
  150. static int
  151. sfspi_xfer_buf(struct sfspi_softc *sc, uint8_t *rxbuf, uint8_t *txbuf,
  152. uint32_t txlen, uint32_t rxlen)
  153. {
  154. uint32_t bytes;
  155. KASSERT(txlen == rxlen, ("TX and RX lengths must be equal"));
  156. KASSERT(rxbuf != NULL, ("RX buffer cannot be NULL"));
  157. KASSERT(txbuf != NULL, ("TX buffer cannot be NULL"));
  158. while (txlen) {
  159. bytes = (txlen > SFSPI_FIFO_DEPTH) ? SFSPI_FIFO_DEPTH : txlen;
  160. sfspi_tx(sc, txbuf, bytes);
  161. txbuf += bytes;
  162. sfspi_rx(sc, rxbuf, bytes);
  163. rxbuf += bytes;
  164. txlen -= bytes;
  165. }
  166. return (0);
  167. }
  168. static int
  169. sfspi_setup(struct sfspi_softc *sc, uint32_t cs, uint32_t mode,
  170. uint32_t freq)
  171. {
  172. uint32_t csmode, fmt, sckdiv, sckmode;
  173. SFSPI_ASSERT_LOCKED(sc);
  174. /*
  175. * Fsck = Fin / 2 * (div + 1)
  176. * -> div = Fin / (2 * Fsck) - 1
  177. */
  178. sckdiv = (howmany(sc->freq >> 1, freq) - 1) & SFSPI_SCKDIV_MASK;
  179. SFSPI_WRITE(sc, SFSPI_REG_SCKDIV, sckdiv);
  180. switch (mode) {
  181. case SPIBUS_MODE_NONE:
  182. sckmode = 0;
  183. break;
  184. case SPIBUS_MODE_CPHA:
  185. sckmode = SFSPI_SCKMODE_PHA;
  186. break;
  187. case SPIBUS_MODE_CPOL:
  188. sckmode = SFSPI_SCKMODE_POL;
  189. break;
  190. case SPIBUS_MODE_CPOL_CPHA:
  191. sckmode = SFSPI_SCKMODE_PHA | SFSPI_SCKMODE_POL;
  192. break;
  193. default:
  194. return (EINVAL);
  195. }
  196. SFSPI_WRITE(sc, SFSPI_REG_SCKMODE, sckmode);
  197. csmode = SFSPI_CSMODE_HOLD;
  198. if (cs & SPIBUS_CS_HIGH)
  199. csmode = SFSPI_CSMODE_AUTO;
  200. SFSPI_WRITE(sc, SFSPI_REG_CSMODE, csmode);
  201. SFSPI_WRITE(sc, SFSPI_REG_CSID, cs & ~SPIBUS_CS_HIGH);
  202. fmt = SFSPI_FMT_PROTO_SINGLE | SFSPI_FMT_LEN(8);
  203. SFSPI_WRITE(sc, SFSPI_REG_FMT, fmt);
  204. return (0);
  205. }
  206. static int
  207. sfspi_transfer(device_t dev, device_t child, struct spi_command *cmd)
  208. {
  209. struct sfspi_softc *sc;
  210. uint32_t clock, cs, csdef, mode;
  211. int err;
  212. KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,
  213. ("TX and RX command sizes must be equal"));
  214. KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,
  215. ("TX and RX data sizes must be equal"));
  216. sc = device_get_softc(dev);
  217. spibus_get_cs(child, &cs);
  218. spibus_get_clock(child, &clock);
  219. spibus_get_mode(child, &mode);
  220. if (cs > sc->cs_max) {
  221. device_printf(sc->dev, "Invalid chip select %u\n", cs);
  222. return (EINVAL);
  223. }
  224. SFSPI_LOCK(sc);
  225. device_busy(sc->dev);
  226. err = sfspi_setup(sc, cs, mode, clock);
  227. if (err != 0) {
  228. SFSPI_UNLOCK(sc);
  229. return (err);
  230. }
  231. err = 0;
  232. if (cmd->tx_cmd_sz > 0)
  233. err = sfspi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd,
  234. cmd->tx_cmd_sz, cmd->rx_cmd_sz);
  235. if (cmd->tx_data_sz > 0 && err == 0)
  236. err = sfspi_xfer_buf(sc, cmd->rx_data, cmd->tx_data,
  237. cmd->tx_data_sz, cmd->rx_data_sz);
  238. /* Deassert chip select. */
  239. csdef = SFSPI_CSDEF_ALL & ~(1 << cs);
  240. SFSPI_WRITE(sc, SFSPI_REG_CSDEF, csdef);
  241. SFSPI_WRITE(sc, SFSPI_REG_CSDEF, SFSPI_CSDEF_ALL);
  242. device_unbusy(sc->dev);
  243. SFSPI_UNLOCK(sc);
  244. return (err);
  245. }
  246. static int
  247. sfspi_attach(device_t dev)
  248. {
  249. struct sfspi_softc *sc;
  250. int error;
  251. sc = device_get_softc(dev);
  252. sc->dev = dev;
  253. mtx_init(&sc->mtx, device_get_nameunit(sc->dev), NULL, MTX_DEF);
  254. error = bus_alloc_resources(dev, sfspi_spec, &sc->res);
  255. if (error) {
  256. device_printf(dev, "Couldn't allocate resources\n");
  257. goto fail;
  258. }
  259. sc->bst = rman_get_bustag(sc->res);
  260. sc->bsh = rman_get_bushandle(sc->res);
  261. error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
  262. if (error) {
  263. device_printf(dev, "Couldn't allocate clock: %d\n", error);
  264. goto fail;
  265. }
  266. error = clk_enable(sc->clk);
  267. if (error) {
  268. device_printf(dev, "Couldn't enable clock: %d\n", error);
  269. goto fail;
  270. }
  271. error = clk_get_freq(sc->clk, &sc->freq);
  272. if (error) {
  273. device_printf(sc->dev, "Couldn't get frequency: %d\n", error);
  274. goto fail;
  275. }
  276. /*
  277. * From Sifive-Unleashed-FU540-C000-v1.0.pdf page 103:
  278. * csdef is cs_width bits wide and all ones on reset.
  279. */
  280. sc->cs_max = SFSPI_READ(sc, SFSPI_REG_CSDEF);
  281. /*
  282. * We don't support the direct-mapped flash interface.
  283. * Disable it.
  284. */
  285. SFSPI_WRITE(sc, SFSPI_REG_FCTRL, 0x0);
  286. /* Probe and attach the spibus when interrupts are available. */
  287. sc->parent = device_add_child(dev, "spibus", DEVICE_UNIT_ANY);
  288. config_intrhook_oneshot((ich_func_t)bus_generic_attach, dev);
  289. return (0);
  290. fail:
  291. bus_release_resources(dev, sfspi_spec, &sc->res);
  292. mtx_destroy(&sc->mtx);
  293. return (error);
  294. }
  295. static int
  296. sfspi_probe(device_t dev)
  297. {
  298. if (!ofw_bus_status_okay(dev))
  299. return (ENXIO);
  300. if (!ofw_bus_is_compatible(dev, "sifive,spi0"))
  301. return (ENXIO);
  302. device_set_desc(dev, "SiFive SPI controller");
  303. return (BUS_PROBE_DEFAULT);
  304. }
  305. static phandle_t
  306. sfspi_get_node(device_t bus, device_t dev)
  307. {
  308. return (ofw_bus_get_node(bus));
  309. }
  310. static device_method_t sfspi_methods[] = {
  311. DEVMETHOD(device_probe, sfspi_probe),
  312. DEVMETHOD(device_attach, sfspi_attach),
  313. DEVMETHOD(spibus_transfer, sfspi_transfer),
  314. DEVMETHOD(ofw_bus_get_node, sfspi_get_node),
  315. DEVMETHOD_END
  316. };
  317. static driver_t sfspi_driver = {
  318. "sifive_spi",
  319. sfspi_methods,
  320. sizeof(struct sfspi_softc)
  321. };
  322. DRIVER_MODULE(sifive_spi, simplebus, sfspi_driver, 0, 0);
  323. DRIVER_MODULE(ofw_spibus, sifive_spi, ofw_spibus_driver, 0, 0);
  324. MODULE_DEPEND(sifive_spi, ofw_spibus, 1, 1, 1);