spi-howto.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. How to port a SPI driver to driver model
  2. ========================================
  3. Here is a rough step-by-step guide. It is based around converting the
  4. exynos SPI driver to driver model (DM) and the example code is based
  5. around U-Boot v2014.10-rc2 (commit be9f643).
  6. It is quite long since it includes actual code examples.
  7. Before driver model, SPI drivers have their own private structure which
  8. contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
  9. exists, but now it is 'per-child data' for the SPI bus. Each child of the
  10. SPI bus is a SPI slave. The information that was stored in the
  11. driver-specific slave structure can now be port in private data for the
  12. SPI bus.
  13. For example, struct tegra_spi_slave looks like this:
  14. struct tegra_spi_slave {
  15. struct spi_slave slave;
  16. struct tegra_spi_ctrl *ctrl;
  17. };
  18. In this case 'slave' will be in per-child data, and 'ctrl' will be in the
  19. SPI's buses private data.
  20. 0. How long does this take?
  21. You should be able to complete this within 2 hours, including testing but
  22. excluding preparing the patches. The API is basically the same as before
  23. with only minor changes:
  24. - methods to set speed and mode are separated out
  25. - cs_info is used to get information on a chip select
  26. 1. Enable driver mode for SPI and SPI flash
  27. Add these to your board config:
  28. #define CONFIG_DM_SPI
  29. #define CONFIG_DM_SPI_FLASH
  30. 2. Add the skeleton
  31. Put this code at the bottom of your existing driver file:
  32. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  33. unsigned int max_hz, unsigned int mode)
  34. {
  35. return NULL;
  36. }
  37. struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
  38. int spi_node)
  39. {
  40. return NULL;
  41. }
  42. static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
  43. {
  44. return -ENODEV;
  45. }
  46. static int exynos_spi_probe(struct udevice *dev)
  47. {
  48. return -ENODEV;
  49. }
  50. static int exynos_spi_remove(struct udevice *dev)
  51. {
  52. return -ENODEV;
  53. }
  54. static int exynos_spi_claim_bus(struct udevice *dev)
  55. {
  56. return -ENODEV;
  57. }
  58. static int exynos_spi_release_bus(struct udevice *dev)
  59. {
  60. return -ENODEV;
  61. }
  62. static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
  63. const void *dout, void *din, unsigned long flags)
  64. {
  65. return -ENODEV;
  66. }
  67. static int exynos_spi_set_speed(struct udevice *dev, uint speed)
  68. {
  69. return -ENODEV;
  70. }
  71. static int exynos_spi_set_mode(struct udevice *dev, uint mode)
  72. {
  73. return -ENODEV;
  74. }
  75. static int exynos_cs_info(struct udevice *bus, uint cs,
  76. struct spi_cs_info *info)
  77. {
  78. return -ENODEV;
  79. }
  80. static const struct dm_spi_ops exynos_spi_ops = {
  81. .claim_bus = exynos_spi_claim_bus,
  82. .release_bus = exynos_spi_release_bus,
  83. .xfer = exynos_spi_xfer,
  84. .set_speed = exynos_spi_set_speed,
  85. .set_mode = exynos_spi_set_mode,
  86. .cs_info = exynos_cs_info,
  87. };
  88. static const struct udevice_id exynos_spi_ids[] = {
  89. { .compatible = "samsung,exynos-spi" },
  90. { }
  91. };
  92. U_BOOT_DRIVER(exynos_spi) = {
  93. .name = "exynos_spi",
  94. .id = UCLASS_SPI,
  95. .of_match = exynos_spi_ids,
  96. .ops = &exynos_spi_ops,
  97. .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
  98. .probe = exynos_spi_probe,
  99. .remove = exynos_spi_remove,
  100. };
  101. 3. Replace 'exynos' in the above code with your driver name
  102. 4. #ifdef out all of the code in your driver except for the above
  103. This will allow you to get it building, which means you can work
  104. incrementally. Since all the methods return an error initially, there is
  105. less chance that you will accidentally leave something in.
  106. Also, even though your conversion is basically a rewrite, it might help
  107. reviewers if you leave functions in the same place in the file,
  108. particularly for large drivers.
  109. 5. Add some includes
  110. Add these includes to your driver:
  111. #include <dm.h>
  112. #include <errno.h>
  113. 6. Build
  114. At this point you should be able to build U-Boot for your board with the
  115. empty SPI driver. You still have empty methods in your driver, but we will
  116. write these one by one.
  117. If you have spi_init() functions or the like that are called from your
  118. board then the build will fail. Remove these calls and make a note of the
  119. init that needs to be done.
  120. 7. Set up your platform data structure
  121. This will hold the information your driver to operate, like its hardware
  122. address or maximum frequency.
  123. You may already have a struct like this, or you may need to create one
  124. from some of the #defines or global variables in the driver.
  125. Note that this information is not the run-time information. It should not
  126. include state that changes. It should be fixed throughout the live of
  127. U-Boot. Run-time information comes later.
  128. Here is what was in the exynos spi driver:
  129. struct spi_bus {
  130. enum periph_id periph_id;
  131. s32 frequency; /* Default clock frequency, -1 for none */
  132. struct exynos_spi *regs;
  133. int inited; /* 1 if this bus is ready for use */
  134. int node;
  135. uint deactivate_delay_us; /* Delay to wait after deactivate */
  136. };
  137. Of these, inited is handled by DM and node is the device tree node, which
  138. DM tells you. The name is not quite right. So in this case we would use:
  139. struct exynos_spi_platdata {
  140. enum periph_id periph_id;
  141. s32 frequency; /* Default clock frequency, -1 for none */
  142. struct exynos_spi *regs;
  143. uint deactivate_delay_us; /* Delay to wait after deactivate */
  144. };
  145. 8a. Write ofdata_to_platdata() [for device tree only]
  146. This method will convert information in the device tree node into a C
  147. structure in your driver (called platform data). If you are not using
  148. device tree, go to 8b.
  149. DM will automatically allocate the struct for us when we are using device
  150. tree, but we need to tell it the size:
  151. U_BOOT_DRIVER(spi_exynos) = {
  152. ...
  153. .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
  154. Here is a sample function. It gets a pointer to the platform data and
  155. fills in the fields from device tree.
  156. static int exynos_spi_ofdata_to_platdata(struct udevice *bus)
  157. {
  158. struct exynos_spi_platdata *plat = bus->platdata;
  159. const void *blob = gd->fdt_blob;
  160. int node = bus->of_offset;
  161. plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  162. plat->periph_id = pinmux_decode_periph_id(blob, node);
  163. if (plat->periph_id == PERIPH_ID_NONE) {
  164. debug("%s: Invalid peripheral ID %d\n", __func__,
  165. plat->periph_id);
  166. return -FDT_ERR_NOTFOUND;
  167. }
  168. /* Use 500KHz as a suitable default */
  169. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  170. 500000);
  171. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  172. "spi-deactivate-delay", 0);
  173. debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
  174. __func__, plat->regs, plat->periph_id, plat->frequency,
  175. plat->deactivate_delay_us);
  176. return 0;
  177. }
  178. 8b. Add the platform data [non-device-tree only]
  179. Specify this data in a U_BOOT_DEVICE() declaration in your board file:
  180. struct exynos_spi_platdata platdata_spi0 = {
  181. .periph_id = ...
  182. .frequency = ...
  183. .regs = ...
  184. .deactivate_delay_us = ...
  185. };
  186. U_BOOT_DEVICE(board_spi0) = {
  187. .name = "exynos_spi",
  188. .platdata = &platdata_spi0,
  189. };
  190. You will unfortunately need to put the struct into a header file in this
  191. case so that your board file can use it.
  192. 9. Add the device private data
  193. Most devices have some private data which they use to keep track of things
  194. while active. This is the run-time information and needs to be stored in
  195. a structure. There is probably a structure in the driver that includes a
  196. 'struct spi_slave', so you can use that.
  197. struct exynos_spi_slave {
  198. struct spi_slave slave;
  199. struct exynos_spi *regs;
  200. unsigned int freq; /* Default frequency */
  201. unsigned int mode;
  202. enum periph_id periph_id; /* Peripheral ID for this device */
  203. unsigned int fifo_size;
  204. int skip_preamble;
  205. struct spi_bus *bus; /* Pointer to our SPI bus info */
  206. ulong last_transaction_us; /* Time of last transaction end */
  207. };
  208. We should rename this to make its purpose more obvious, and get rid of
  209. the slave structure, so we have:
  210. struct exynos_spi_priv {
  211. struct exynos_spi *regs;
  212. unsigned int freq; /* Default frequency */
  213. unsigned int mode;
  214. enum periph_id periph_id; /* Peripheral ID for this device */
  215. unsigned int fifo_size;
  216. int skip_preamble;
  217. ulong last_transaction_us; /* Time of last transaction end */
  218. };
  219. DM can auto-allocate this also:
  220. U_BOOT_DRIVER(spi_exynos) = {
  221. ...
  222. .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
  223. Note that this is created before the probe method is called, and destroyed
  224. after the remove method is called. It will be zeroed when the probe
  225. method is called.
  226. 10. Add the probe() and remove() methods
  227. Note: It's a good idea to build repeatedly as you are working, to avoid a
  228. huge amount of work getting things compiling at the end.
  229. The probe method is supposed to set up the hardware. U-Boot used to use
  230. spi_setup_slave() to do this. So take a look at this function and see
  231. what you can copy out to set things up.
  232. static int exynos_spi_probe(struct udevice *bus)
  233. {
  234. struct exynos_spi_platdata *plat = dev_get_platdata(bus);
  235. struct exynos_spi_priv *priv = dev_get_priv(bus);
  236. priv->regs = plat->regs;
  237. if (plat->periph_id == PERIPH_ID_SPI1 ||
  238. plat->periph_id == PERIPH_ID_SPI2)
  239. priv->fifo_size = 64;
  240. else
  241. priv->fifo_size = 256;
  242. priv->skip_preamble = 0;
  243. priv->last_transaction_us = timer_get_us();
  244. priv->freq = plat->frequency;
  245. priv->periph_id = plat->periph_id;
  246. return 0;
  247. }
  248. This implementation doesn't actually touch the hardware, which is somewhat
  249. unusual for a driver. In this case we will do that when the device is
  250. claimed by something that wants to use the SPI bus.
  251. For remove we could shut down the clocks, but in this case there is
  252. nothing to do. DM frees any memory that it allocated, so we can just
  253. remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
  254. 11. Implement set_speed()
  255. This should set up clocks so that the SPI bus is running at the right
  256. speed. With the old API spi_claim_bus() would normally do this and several
  257. of the following functions, so let's look at that function:
  258. int spi_claim_bus(struct spi_slave *slave)
  259. {
  260. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  261. struct exynos_spi *regs = spi_slave->regs;
  262. u32 reg = 0;
  263. int ret;
  264. ret = set_spi_clk(spi_slave->periph_id,
  265. spi_slave->freq);
  266. if (ret < 0) {
  267. debug("%s: Failed to setup spi clock\n", __func__);
  268. return ret;
  269. }
  270. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  271. spi_flush_fifo(slave);
  272. reg = readl(&regs->ch_cfg);
  273. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  274. if (spi_slave->mode & SPI_CPHA)
  275. reg |= SPI_CH_CPHA_B;
  276. if (spi_slave->mode & SPI_CPOL)
  277. reg |= SPI_CH_CPOL_L;
  278. writel(reg, &regs->ch_cfg);
  279. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  280. return 0;
  281. }
  282. It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
  283. With DM these will happen in separate methods.
  284. Here is an example for the speed part:
  285. static int exynos_spi_set_speed(struct udevice *bus, uint speed)
  286. {
  287. struct exynos_spi_platdata *plat = bus->platdata;
  288. struct exynos_spi_priv *priv = dev_get_priv(bus);
  289. int ret;
  290. if (speed > plat->frequency)
  291. speed = plat->frequency;
  292. ret = set_spi_clk(priv->periph_id, speed);
  293. if (ret)
  294. return ret;
  295. priv->freq = speed;
  296. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  297. return 0;
  298. }
  299. 12. Implement set_mode()
  300. This should adjust the SPI mode (polarity, etc.). Again this code probably
  301. comes from the old spi_claim_bus(). Here is an example:
  302. static int exynos_spi_set_mode(struct udevice *bus, uint mode)
  303. {
  304. struct exynos_spi_priv *priv = dev_get_priv(bus);
  305. uint32_t reg;
  306. reg = readl(&priv->regs->ch_cfg);
  307. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  308. if (mode & SPI_CPHA)
  309. reg |= SPI_CH_CPHA_B;
  310. if (mode & SPI_CPOL)
  311. reg |= SPI_CH_CPOL_L;
  312. writel(reg, &priv->regs->ch_cfg);
  313. priv->mode = mode;
  314. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  315. return 0;
  316. }
  317. 13. Implement claim_bus()
  318. This is where a client wants to make use of the bus, so claims it first.
  319. At this point we need to make sure everything is set up ready for data
  320. transfer. Note that this function is wholly internal to the driver - at
  321. present the SPI uclass never calls it.
  322. Here again we look at the old claim function and see some code that is
  323. needed. It is anything unrelated to speed and mode:
  324. static int exynos_spi_claim_bus(struct udevice *bus)
  325. {
  326. struct exynos_spi_priv *priv = dev_get_priv(bus);
  327. exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
  328. spi_flush_fifo(priv->regs);
  329. writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
  330. return 0;
  331. }
  332. The spi_flush_fifo() function is in the removed part of the code, so we
  333. need to expose it again (perhaps with an #endif before it and '#if 0'
  334. after it). It only needs access to priv->regs which is why we have
  335. passed that in:
  336. /**
  337. * Flush spi tx, rx fifos and reset the SPI controller
  338. *
  339. * @param regs Pointer to SPI registers
  340. */
  341. static void spi_flush_fifo(struct exynos_spi *regs)
  342. {
  343. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  344. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  345. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  346. }
  347. 14. Implement release_bus()
  348. This releases the bus - in our example the old code in spi_release_bus()
  349. is a call to spi_flush_fifo, so we add:
  350. static int exynos_spi_release_bus(struct udevice *bus)
  351. {
  352. struct exynos_spi_priv *priv = dev_get_priv(bus);
  353. spi_flush_fifo(priv->regs);
  354. return 0;
  355. }
  356. 15. Implement xfer()
  357. This is the final method that we need to create, and it is where all the
  358. work happens. The method parameters are the same as the old spi_xfer() with
  359. the addition of a 'struct udevice' so conversion is pretty easy. Start
  360. by copying the contents of spi_xfer() to your new xfer() method and proceed
  361. from there.
  362. If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
  363. activate function, something like this:
  364. void spi_cs_activate(struct spi_slave *slave)
  365. {
  366. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  367. /* If it's too soon to do another transaction, wait */
  368. if (spi_slave->bus->deactivate_delay_us &&
  369. spi_slave->last_transaction_us) {
  370. ulong delay_us; /* The delay completed so far */
  371. delay_us = timer_get_us() - spi_slave->last_transaction_us;
  372. if (delay_us < spi_slave->bus->deactivate_delay_us)
  373. udelay(spi_slave->bus->deactivate_delay_us - delay_us);
  374. }
  375. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  376. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  377. spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
  378. }
  379. The new version looks like this:
  380. static void spi_cs_activate(struct udevice *dev)
  381. {
  382. struct udevice *bus = dev->parent;
  383. struct exynos_spi_platdata *pdata = dev_get_platdata(bus);
  384. struct exynos_spi_priv *priv = dev_get_priv(bus);
  385. /* If it's too soon to do another transaction, wait */
  386. if (pdata->deactivate_delay_us &&
  387. priv->last_transaction_us) {
  388. ulong delay_us; /* The delay completed so far */
  389. delay_us = timer_get_us() - priv->last_transaction_us;
  390. if (delay_us < pdata->deactivate_delay_us)
  391. udelay(pdata->deactivate_delay_us - delay_us);
  392. }
  393. clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  394. debug("Activate CS, bus '%s'\n", bus->name);
  395. priv->skip_preamble = priv->mode & SPI_PREAMBLE;
  396. }
  397. All we have really done here is change the pointers and print the device name
  398. instead of the bus number. Other local static functions can be treated in
  399. the same way.
  400. 16. Set up the per-child data and child pre-probe function
  401. To minimise the pain and complexity of the SPI subsystem while the driver
  402. model change-over is in place, struct spi_slave is used to reference a
  403. SPI bus slave, even though that slave is actually a struct udevice. In fact
  404. struct spi_slave is the device's child data. We need to make sure this space
  405. is available. It is possible to allocate more space that struct spi_slave
  406. needs, but this is the minimum.
  407. U_BOOT_DRIVER(exynos_spi) = {
  408. ...
  409. .per_child_auto_alloc_size = sizeof(struct spi_slave),
  410. }
  411. 17. Optional: Set up cs_info() if you want it
  412. Sometimes it is useful to know whether a SPI chip select is valid, but this
  413. is not obvious from outside the driver. In this case you can provide a
  414. method for cs_info() to deal with this. If you don't provide it, then the
  415. device tree will be used to determine what chip selects are valid.
  416. Return -ENODEV if the supplied chip select is invalid, or 0 if it is valid.
  417. If you don't provide the cs_info() method, -ENODEV is assumed for all
  418. chip selects that do not appear in the device tree.
  419. 18. Test it
  420. Now that you have the code written and it compiles, try testing it using
  421. the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
  422. board.
  423. 19. Prepare patches and send them to the mailing lists
  424. You can use 'tools/patman/patman' to prepare, check and send patches for
  425. your work. See the README for details.