1 How to port a SPI driver to driver model
2 ========================================
4 Here is a rough step-by-step guide. It is based around converting the
5 exynos SPI driver to driver model (DM) and the example code is based
6 around U-Boot v2014.10-rc2 (commit be9f643).
8 It is quite long since it includes actual code examples.
10 Before driver model, SPI drivers have their own private structure which
11 contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
12 exists, but now it is 'per-child data' for the SPI bus. Each child of the
13 SPI bus is a SPI slave. The information that was stored in the
14 driver-specific slave structure can now be port in private data for the
17 For example, struct tegra_spi_slave looks like this:
19 struct tegra_spi_slave {
20 struct spi_slave slave;
21 struct tegra_spi_ctrl *ctrl;
24 In this case 'slave' will be in per-child data, and 'ctrl' will be in the
25 SPI's buses private data.
28 0. How long does this take?
30 You should be able to complete this within 2 hours, including testing but
31 excluding preparing the patches. The API is basically the same as before
32 with only minor changes:
34 - methods to set speed and mode are separated out
35 - cs_info is used to get information on a chip select
38 1. Enable driver mode for SPI and SPI flash
40 Add these to your board config:
43 #define CONFIG_DM_SPI_FLASH
48 Put this code at the bottom of your existing driver file:
50 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
51 unsigned int max_hz, unsigned int mode)
56 struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
62 static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
67 static int exynos_spi_probe(struct udevice *dev)
72 static int exynos_spi_remove(struct udevice *dev)
77 static int exynos_spi_claim_bus(struct udevice *dev)
83 static int exynos_spi_release_bus(struct udevice *dev)
89 static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
90 const void *dout, void *din, unsigned long flags)
96 static int exynos_spi_set_speed(struct udevice *dev, uint speed)
101 static int exynos_spi_set_mode(struct udevice *dev, uint mode)
106 static int exynos_cs_info(struct udevice *bus, uint cs,
107 struct spi_cs_info *info)
112 static const struct dm_spi_ops exynos_spi_ops = {
113 .claim_bus = exynos_spi_claim_bus,
114 .release_bus = exynos_spi_release_bus,
115 .xfer = exynos_spi_xfer,
116 .set_speed = exynos_spi_set_speed,
117 .set_mode = exynos_spi_set_mode,
118 .cs_info = exynos_cs_info,
121 static const struct udevice_id exynos_spi_ids[] = {
122 { .compatible = "samsung,exynos-spi" },
126 U_BOOT_DRIVER(exynos_spi) = {
127 .name = "exynos_spi",
129 .of_match = exynos_spi_ids,
130 .ops = &exynos_spi_ops,
131 .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
132 .probe = exynos_spi_probe,
133 .remove = exynos_spi_remove,
137 3. Replace 'exynos' in the above code with your driver name
140 4. #ifdef out all of the code in your driver except for the above
142 This will allow you to get it building, which means you can work
143 incrementally. Since all the methods return an error initially, there is
144 less chance that you will accidentally leave something in.
146 Also, even though your conversion is basically a rewrite, it might help
147 reviewers if you leave functions in the same place in the file,
148 particularly for large drivers.
153 Add these includes to your driver:
161 At this point you should be able to build U-Boot for your board with the
162 empty SPI driver. You still have empty methods in your driver, but we will
163 write these one by one.
165 If you have spi_init() functions or the like that are called from your
166 board then the build will fail. Remove these calls and make a note of the
167 init that needs to be done.
170 7. Set up your platform data structure
172 This will hold the information your driver to operate, like its hardware
173 address or maximum frequency.
175 You may already have a struct like this, or you may need to create one
176 from some of the #defines or global variables in the driver.
178 Note that this information is not the run-time information. It should not
179 include state that changes. It should be fixed throughout the live of
180 U-Boot. Run-time information comes later.
182 Here is what was in the exynos spi driver:
185 enum periph_id periph_id;
186 s32 frequency; /* Default clock frequency, -1 for none */
187 struct exynos_spi *regs;
188 int inited; /* 1 if this bus is ready for use */
190 uint deactivate_delay_us; /* Delay to wait after deactivate */
193 Of these, inited is handled by DM and node is the device tree node, which
194 DM tells you. The name is not quite right. So in this case we would use:
196 struct exynos_spi_platdata {
197 enum periph_id periph_id;
198 s32 frequency; /* Default clock frequency, -1 for none */
199 struct exynos_spi *regs;
200 uint deactivate_delay_us; /* Delay to wait after deactivate */
204 8a. Write ofdata_to_platdata() [for device tree only]
206 This method will convert information in the device tree node into a C
207 structure in your driver (called platform data). If you are not using
208 device tree, go to 8b.
210 DM will automatically allocate the struct for us when we are using device
211 tree, but we need to tell it the size:
213 U_BOOT_DRIVER(spi_exynos) = {
215 .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
218 Here is a sample function. It gets a pointer to the platform data and
219 fills in the fields from device tree.
221 static int exynos_spi_ofdata_to_platdata(struct udevice *bus)
223 struct exynos_spi_platdata *plat = bus->platdata;
224 const void *blob = gd->fdt_blob;
225 int node = bus->of_offset;
227 plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
228 plat->periph_id = pinmux_decode_periph_id(blob, node);
230 if (plat->periph_id == PERIPH_ID_NONE) {
231 debug("%s: Invalid peripheral ID %d\n", __func__,
233 return -FDT_ERR_NOTFOUND;
236 /* Use 500KHz as a suitable default */
237 plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
239 plat->deactivate_delay_us = fdtdec_get_int(blob, node,
240 "spi-deactivate-delay", 0);
241 debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
242 __func__, plat->regs, plat->periph_id, plat->frequency,
243 plat->deactivate_delay_us);
249 8b. Add the platform data [non-device-tree only]
251 Specify this data in a U_BOOT_DEVICE() declaration in your board file:
253 struct exynos_spi_platdata platdata_spi0 = {
257 .deactivate_delay_us = ...
260 U_BOOT_DEVICE(board_spi0) = {
261 .name = "exynos_spi",
262 .platdata = &platdata_spi0,
265 You will unfortunately need to put the struct into a header file in this
266 case so that your board file can use it.
269 9. Add the device private data
271 Most devices have some private data which they use to keep track of things
272 while active. This is the run-time information and needs to be stored in
273 a structure. There is probably a structure in the driver that includes a
274 'struct spi_slave', so you can use that.
276 struct exynos_spi_slave {
277 struct spi_slave slave;
278 struct exynos_spi *regs;
279 unsigned int freq; /* Default frequency */
281 enum periph_id periph_id; /* Peripheral ID for this device */
282 unsigned int fifo_size;
284 struct spi_bus *bus; /* Pointer to our SPI bus info */
285 ulong last_transaction_us; /* Time of last transaction end */
289 We should rename this to make its purpose more obvious, and get rid of
290 the slave structure, so we have:
292 struct exynos_spi_priv {
293 struct exynos_spi *regs;
294 unsigned int freq; /* Default frequency */
296 enum periph_id periph_id; /* Peripheral ID for this device */
297 unsigned int fifo_size;
299 ulong last_transaction_us; /* Time of last transaction end */
303 DM can auto-allocate this also:
305 U_BOOT_DRIVER(spi_exynos) = {
307 .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
310 Note that this is created before the probe method is called, and destroyed
311 after the remove method is called. It will be zeroed when the probe
315 10. Add the probe() and remove() methods
317 Note: It's a good idea to build repeatedly as you are working, to avoid a
318 huge amount of work getting things compiling at the end.
320 The probe method is supposed to set up the hardware. U-Boot used to use
321 spi_setup_slave() to do this. So take a look at this function and see
322 what you can copy out to set things up.
325 static int exynos_spi_probe(struct udevice *bus)
327 struct exynos_spi_platdata *plat = dev_get_platdata(bus);
328 struct exynos_spi_priv *priv = dev_get_priv(bus);
330 priv->regs = plat->regs;
331 if (plat->periph_id == PERIPH_ID_SPI1 ||
332 plat->periph_id == PERIPH_ID_SPI2)
333 priv->fifo_size = 64;
335 priv->fifo_size = 256;
337 priv->skip_preamble = 0;
338 priv->last_transaction_us = timer_get_us();
339 priv->freq = plat->frequency;
340 priv->periph_id = plat->periph_id;
345 This implementation doesn't actually touch the hardware, which is somewhat
346 unusual for a driver. In this case we will do that when the device is
347 claimed by something that wants to use the SPI bus.
349 For remove we could shut down the clocks, but in this case there is
350 nothing to do. DM frees any memory that it allocated, so we can just
351 remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
354 11. Implement set_speed()
356 This should set up clocks so that the SPI bus is running at the right
357 speed. With the old API spi_claim_bus() would normally do this and several
358 of the following functions, so let's look at that function:
360 int spi_claim_bus(struct spi_slave *slave)
362 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
363 struct exynos_spi *regs = spi_slave->regs;
367 ret = set_spi_clk(spi_slave->periph_id,
370 debug("%s: Failed to setup spi clock\n", __func__);
374 exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
376 spi_flush_fifo(slave);
378 reg = readl(®s->ch_cfg);
379 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
381 if (spi_slave->mode & SPI_CPHA)
382 reg |= SPI_CH_CPHA_B;
384 if (spi_slave->mode & SPI_CPOL)
385 reg |= SPI_CH_CPOL_L;
387 writel(reg, ®s->ch_cfg);
388 writel(SPI_FB_DELAY_180, ®s->fb_clk);
394 It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
395 With DM these will happen in separate methods.
398 Here is an example for the speed part:
400 static int exynos_spi_set_speed(struct udevice *bus, uint speed)
402 struct exynos_spi_platdata *plat = bus->platdata;
403 struct exynos_spi_priv *priv = dev_get_priv(bus);
406 if (speed > plat->frequency)
407 speed = plat->frequency;
408 ret = set_spi_clk(priv->periph_id, speed);
412 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
418 12. Implement set_mode()
420 This should adjust the SPI mode (polarity, etc.). Again this code probably
421 comes from the old spi_claim_bus(). Here is an example:
424 static int exynos_spi_set_mode(struct udevice *bus, uint mode)
426 struct exynos_spi_priv *priv = dev_get_priv(bus);
429 reg = readl(&priv->regs->ch_cfg);
430 reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
433 reg |= SPI_CH_CPHA_B;
436 reg |= SPI_CH_CPOL_L;
438 writel(reg, &priv->regs->ch_cfg);
440 debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
446 13. Implement claim_bus()
448 This is where a client wants to make use of the bus, so claims it first.
449 At this point we need to make sure everything is set up ready for data
450 transfer. Note that this function is wholly internal to the driver - at
451 present the SPI uclass never calls it.
453 Here again we look at the old claim function and see some code that is
454 needed. It is anything unrelated to speed and mode:
456 static int exynos_spi_claim_bus(struct udevice *bus)
458 struct exynos_spi_priv *priv = dev_get_priv(bus);
460 exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
461 spi_flush_fifo(priv->regs);
463 writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
468 The spi_flush_fifo() function is in the removed part of the code, so we
469 need to expose it again (perhaps with an #endif before it and '#if 0'
470 after it). It only needs access to priv->regs which is why we have
474 * Flush spi tx, rx fifos and reset the SPI controller
476 * @param regs Pointer to SPI registers
478 static void spi_flush_fifo(struct exynos_spi *regs)
480 clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
481 clrbits_le32(®s->ch_cfg, SPI_CH_RST);
482 setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
486 14. Implement release_bus()
488 This releases the bus - in our example the old code in spi_release_bus()
489 is a call to spi_flush_fifo, so we add:
491 static int exynos_spi_release_bus(struct udevice *bus)
493 struct exynos_spi_priv *priv = dev_get_priv(bus);
495 spi_flush_fifo(priv->regs);
503 This is the final method that we need to create, and it is where all the
504 work happens. The method parameters are the same as the old spi_xfer() with
505 the addition of a 'struct udevice' so conversion is pretty easy. Start
506 by copying the contents of spi_xfer() to your new xfer() method and proceed
509 If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
510 activate function, something like this:
512 void spi_cs_activate(struct spi_slave *slave)
514 struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
516 /* If it's too soon to do another transaction, wait */
517 if (spi_slave->bus->deactivate_delay_us &&
518 spi_slave->last_transaction_us) {
519 ulong delay_us; /* The delay completed so far */
520 delay_us = timer_get_us() - spi_slave->last_transaction_us;
521 if (delay_us < spi_slave->bus->deactivate_delay_us)
522 udelay(spi_slave->bus->deactivate_delay_us - delay_us);
525 clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
526 debug("Activate CS, bus %d\n", spi_slave->slave.bus);
527 spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
530 The new version looks like this:
532 static void spi_cs_activate(struct udevice *dev)
534 struct udevice *bus = dev->parent;
535 struct exynos_spi_platdata *pdata = dev_get_platdata(bus);
536 struct exynos_spi_priv *priv = dev_get_priv(bus);
538 /* If it's too soon to do another transaction, wait */
539 if (pdata->deactivate_delay_us &&
540 priv->last_transaction_us) {
541 ulong delay_us; /* The delay completed so far */
542 delay_us = timer_get_us() - priv->last_transaction_us;
543 if (delay_us < pdata->deactivate_delay_us)
544 udelay(pdata->deactivate_delay_us - delay_us);
547 clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
548 debug("Activate CS, bus '%s'\n", bus->name);
549 priv->skip_preamble = priv->mode & SPI_PREAMBLE;
552 All we have really done here is change the pointers and print the device name
553 instead of the bus number. Other local static functions can be treated in
557 16. Set up the per-child data and child pre-probe function
559 To minimise the pain and complexity of the SPI subsystem while the driver
560 model change-over is in place, struct spi_slave is used to reference a
561 SPI bus slave, even though that slave is actually a struct udevice. In fact
562 struct spi_slave is the device's child data. We need to make sure this space
563 is available. It is possible to allocate more space that struct spi_slave
564 needs, but this is the minimum.
566 U_BOOT_DRIVER(exynos_spi) = {
568 .per_child_auto_alloc_size = sizeof(struct spi_slave),
572 17. Optional: Set up cs_info() if you want it
574 Sometimes it is useful to know whether a SPI chip select is valid, but this
575 is not obvious from outside the driver. In this case you can provide a
576 method for cs_info() to deal with this. If you don't provide it, then the
577 device tree will be used to determine what chip selects are valid.
579 Return -ENODEV if the supplied chip select is invalid, or 0 if it is valid.
580 If you don't provide the cs_info() method, -ENODEV is assumed for all
581 chip selects that do not appear in the device tree.
586 Now that you have the code written and it compiles, try testing it using
587 the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
591 19. Prepare patches and send them to the mailing lists
593 You can use 'tools/patman/patman' to prepare, check and send patches for
594 your work. See the README for details.