1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0+ 2*4882a593Smuzhiyun 3*4882a593SmuzhiyunHow to port a SPI driver to driver model 4*4882a593Smuzhiyun======================================== 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunHere is a rough step-by-step guide. It is based around converting the 7*4882a593Smuzhiyunexynos SPI driver to driver model (DM) and the example code is based 8*4882a593Smuzhiyunaround U-Boot v2014.10-rc2 (commit be9f643). This has been updated for 9*4882a593Smuzhiyunv2015.04. 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunIt is quite long since it includes actual code examples. 12*4882a593Smuzhiyun 13*4882a593SmuzhiyunBefore driver model, SPI drivers have their own private structure which 14*4882a593Smuzhiyuncontains 'struct spi_slave'. With driver model, 'struct spi_slave' still 15*4882a593Smuzhiyunexists, but now it is 'per-child data' for the SPI bus. Each child of the 16*4882a593SmuzhiyunSPI bus is a SPI slave. The information that was stored in the 17*4882a593Smuzhiyundriver-specific slave structure can now be port in private data for the 18*4882a593SmuzhiyunSPI bus. 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunFor example, struct tegra_spi_slave looks like this: 21*4882a593Smuzhiyun 22*4882a593Smuzhiyun.. code-block:: c 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun struct tegra_spi_slave { 25*4882a593Smuzhiyun struct spi_slave slave; 26*4882a593Smuzhiyun struct tegra_spi_ctrl *ctrl; 27*4882a593Smuzhiyun }; 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunIn this case 'slave' will be in per-child data, and 'ctrl' will be in the 30*4882a593SmuzhiyunSPI's buses private data. 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun 33*4882a593SmuzhiyunHow long does this take? 34*4882a593Smuzhiyun------------------------ 35*4882a593Smuzhiyun 36*4882a593SmuzhiyunYou should be able to complete this within 2 hours, including testing but 37*4882a593Smuzhiyunexcluding preparing the patches. The API is basically the same as before 38*4882a593Smuzhiyunwith only minor changes: 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun- methods to set speed and mode are separated out 41*4882a593Smuzhiyun- cs_info is used to get information on a chip select 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunEnable driver mode for SPI and SPI flash 45*4882a593Smuzhiyun---------------------------------------- 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunAdd these to your board config: 48*4882a593Smuzhiyun 49*4882a593Smuzhiyun* CONFIG_DM_SPI 50*4882a593Smuzhiyun* CONFIG_DM_SPI_FLASH 51*4882a593Smuzhiyun 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunAdd the skeleton 54*4882a593Smuzhiyun---------------- 55*4882a593Smuzhiyun 56*4882a593SmuzhiyunPut this code at the bottom of your existing driver file: 57*4882a593Smuzhiyun 58*4882a593Smuzhiyun.. code-block:: c 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, 61*4882a593Smuzhiyun unsigned int max_hz, unsigned int mode) 62*4882a593Smuzhiyun { 63*4882a593Smuzhiyun return NULL; 64*4882a593Smuzhiyun } 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node, 67*4882a593Smuzhiyun int spi_node) 68*4882a593Smuzhiyun { 69*4882a593Smuzhiyun return NULL; 70*4882a593Smuzhiyun } 71*4882a593Smuzhiyun 72*4882a593Smuzhiyun static int exynos_spi_ofdata_to_platdata(struct udevice *dev) 73*4882a593Smuzhiyun { 74*4882a593Smuzhiyun return -ENODEV; 75*4882a593Smuzhiyun } 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun static int exynos_spi_probe(struct udevice *dev) 78*4882a593Smuzhiyun { 79*4882a593Smuzhiyun return -ENODEV; 80*4882a593Smuzhiyun } 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun static int exynos_spi_remove(struct udevice *dev) 83*4882a593Smuzhiyun { 84*4882a593Smuzhiyun return -ENODEV; 85*4882a593Smuzhiyun } 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun static int exynos_spi_claim_bus(struct udevice *dev) 88*4882a593Smuzhiyun { 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun return -ENODEV; 91*4882a593Smuzhiyun } 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun static int exynos_spi_release_bus(struct udevice *dev) 94*4882a593Smuzhiyun { 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun return -ENODEV; 97*4882a593Smuzhiyun } 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen, 100*4882a593Smuzhiyun const void *dout, void *din, unsigned long flags) 101*4882a593Smuzhiyun { 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun return -ENODEV; 104*4882a593Smuzhiyun } 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun static int exynos_spi_set_speed(struct udevice *dev, uint speed) 107*4882a593Smuzhiyun { 108*4882a593Smuzhiyun return -ENODEV; 109*4882a593Smuzhiyun } 110*4882a593Smuzhiyun 111*4882a593Smuzhiyun static int exynos_spi_set_mode(struct udevice *dev, uint mode) 112*4882a593Smuzhiyun { 113*4882a593Smuzhiyun return -ENODEV; 114*4882a593Smuzhiyun } 115*4882a593Smuzhiyun 116*4882a593Smuzhiyun static int exynos_cs_info(struct udevice *bus, uint cs, 117*4882a593Smuzhiyun struct spi_cs_info *info) 118*4882a593Smuzhiyun { 119*4882a593Smuzhiyun return -ENODEV; 120*4882a593Smuzhiyun } 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun static const struct dm_spi_ops exynos_spi_ops = { 123*4882a593Smuzhiyun .claim_bus = exynos_spi_claim_bus, 124*4882a593Smuzhiyun .release_bus = exynos_spi_release_bus, 125*4882a593Smuzhiyun .xfer = exynos_spi_xfer, 126*4882a593Smuzhiyun .set_speed = exynos_spi_set_speed, 127*4882a593Smuzhiyun .set_mode = exynos_spi_set_mode, 128*4882a593Smuzhiyun .cs_info = exynos_cs_info, 129*4882a593Smuzhiyun }; 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun static const struct udevice_id exynos_spi_ids[] = { 132*4882a593Smuzhiyun { .compatible = "samsung,exynos-spi" }, 133*4882a593Smuzhiyun { } 134*4882a593Smuzhiyun }; 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun U_BOOT_DRIVER(exynos_spi) = { 137*4882a593Smuzhiyun .name = "exynos_spi", 138*4882a593Smuzhiyun .id = UCLASS_SPI, 139*4882a593Smuzhiyun .of_match = exynos_spi_ids, 140*4882a593Smuzhiyun .ops = &exynos_spi_ops, 141*4882a593Smuzhiyun .ofdata_to_platdata = exynos_spi_ofdata_to_platdata, 142*4882a593Smuzhiyun .probe = exynos_spi_probe, 143*4882a593Smuzhiyun .remove = exynos_spi_remove, 144*4882a593Smuzhiyun }; 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunReplace 'exynos' in the above code with your driver name 148*4882a593Smuzhiyun-------------------------------------------------------- 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun#ifdef out all of the code in your driver except for the above 152*4882a593Smuzhiyun-------------------------------------------------------------- 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunThis will allow you to get it building, which means you can work 155*4882a593Smuzhiyunincrementally. Since all the methods return an error initially, there is 156*4882a593Smuzhiyunless chance that you will accidentally leave something in. 157*4882a593Smuzhiyun 158*4882a593SmuzhiyunAlso, even though your conversion is basically a rewrite, it might help 159*4882a593Smuzhiyunreviewers if you leave functions in the same place in the file, 160*4882a593Smuzhiyunparticularly for large drivers. 161*4882a593Smuzhiyun 162*4882a593Smuzhiyun 163*4882a593SmuzhiyunAdd some includes 164*4882a593Smuzhiyun----------------- 165*4882a593Smuzhiyun 166*4882a593SmuzhiyunAdd these includes to your driver: 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun.. code-block:: c 169*4882a593Smuzhiyun 170*4882a593Smuzhiyun #include <dm.h> 171*4882a593Smuzhiyun #include <errno.h> 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun 174*4882a593SmuzhiyunBuild 175*4882a593Smuzhiyun----- 176*4882a593Smuzhiyun 177*4882a593SmuzhiyunAt this point you should be able to build U-Boot for your board with the 178*4882a593Smuzhiyunempty SPI driver. You still have empty methods in your driver, but we will 179*4882a593Smuzhiyunwrite these one by one. 180*4882a593Smuzhiyun 181*4882a593SmuzhiyunSet up your platform data structure 182*4882a593Smuzhiyun----------------------------------- 183*4882a593Smuzhiyun 184*4882a593SmuzhiyunThis will hold the information your driver to operate, like its hardware 185*4882a593Smuzhiyunaddress or maximum frequency. 186*4882a593Smuzhiyun 187*4882a593SmuzhiyunYou may already have a struct like this, or you may need to create one 188*4882a593Smuzhiyunfrom some of the #defines or global variables in the driver. 189*4882a593Smuzhiyun 190*4882a593SmuzhiyunNote that this information is not the run-time information. It should not 191*4882a593Smuzhiyuninclude state that changes. It should be fixed throughout the live of 192*4882a593SmuzhiyunU-Boot. Run-time information comes later. 193*4882a593Smuzhiyun 194*4882a593SmuzhiyunHere is what was in the exynos spi driver: 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun.. code-block:: c 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun struct spi_bus { 199*4882a593Smuzhiyun enum periph_id periph_id; 200*4882a593Smuzhiyun s32 frequency; /* Default clock frequency, -1 for none */ 201*4882a593Smuzhiyun struct exynos_spi *regs; 202*4882a593Smuzhiyun int inited; /* 1 if this bus is ready for use */ 203*4882a593Smuzhiyun int node; 204*4882a593Smuzhiyun uint deactivate_delay_us; /* Delay to wait after deactivate */ 205*4882a593Smuzhiyun }; 206*4882a593Smuzhiyun 207*4882a593SmuzhiyunOf these, inited is handled by DM and node is the device tree node, which 208*4882a593SmuzhiyunDM tells you. The name is not quite right. So in this case we would use: 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun.. code-block:: c 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun struct exynos_spi_platdata { 213*4882a593Smuzhiyun enum periph_id periph_id; 214*4882a593Smuzhiyun s32 frequency; /* Default clock frequency, -1 for none */ 215*4882a593Smuzhiyun struct exynos_spi *regs; 216*4882a593Smuzhiyun uint deactivate_delay_us; /* Delay to wait after deactivate */ 217*4882a593Smuzhiyun }; 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunWrite ofdata_to_platdata() [for device tree only] 221*4882a593Smuzhiyun------------------------------------------------- 222*4882a593Smuzhiyun 223*4882a593SmuzhiyunThis method will convert information in the device tree node into a C 224*4882a593Smuzhiyunstructure in your driver (called platform data). If you are not using 225*4882a593Smuzhiyundevice tree, go to 8b. 226*4882a593Smuzhiyun 227*4882a593SmuzhiyunDM will automatically allocate the struct for us when we are using device 228*4882a593Smuzhiyuntree, but we need to tell it the size: 229*4882a593Smuzhiyun 230*4882a593Smuzhiyun.. code-block:: c 231*4882a593Smuzhiyun 232*4882a593Smuzhiyun U_BOOT_DRIVER(spi_exynos) = { 233*4882a593Smuzhiyun ... 234*4882a593Smuzhiyun .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata), 235*4882a593Smuzhiyun 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunHere is a sample function. It gets a pointer to the platform data and 238*4882a593Smuzhiyunfills in the fields from device tree. 239*4882a593Smuzhiyun 240*4882a593Smuzhiyun.. code-block:: c 241*4882a593Smuzhiyun 242*4882a593Smuzhiyun static int exynos_spi_ofdata_to_platdata(struct udevice *bus) 243*4882a593Smuzhiyun { 244*4882a593Smuzhiyun struct exynos_spi_platdata *plat = bus->platdata; 245*4882a593Smuzhiyun const void *blob = gd->fdt_blob; 246*4882a593Smuzhiyun int node = dev_of_offset(bus); 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg"); 249*4882a593Smuzhiyun plat->periph_id = pinmux_decode_periph_id(blob, node); 250*4882a593Smuzhiyun 251*4882a593Smuzhiyun if (plat->periph_id == PERIPH_ID_NONE) { 252*4882a593Smuzhiyun debug("%s: Invalid peripheral ID %d\n", __func__, 253*4882a593Smuzhiyun plat->periph_id); 254*4882a593Smuzhiyun return -FDT_ERR_NOTFOUND; 255*4882a593Smuzhiyun } 256*4882a593Smuzhiyun 257*4882a593Smuzhiyun /* Use 500KHz as a suitable default */ 258*4882a593Smuzhiyun plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 259*4882a593Smuzhiyun 500000); 260*4882a593Smuzhiyun plat->deactivate_delay_us = fdtdec_get_int(blob, node, 261*4882a593Smuzhiyun "spi-deactivate-delay", 0); 262*4882a593Smuzhiyun debug("%s: regs=%p, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n", 263*4882a593Smuzhiyun __func__, plat->regs, plat->periph_id, plat->frequency, 264*4882a593Smuzhiyun plat->deactivate_delay_us); 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun return 0; 267*4882a593Smuzhiyun } 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun 270*4882a593SmuzhiyunAdd the platform data [non-device-tree only] 271*4882a593Smuzhiyun-------------------------------------------- 272*4882a593Smuzhiyun 273*4882a593SmuzhiyunSpecify this data in a U_BOOT_DEVICE() declaration in your board file: 274*4882a593Smuzhiyun 275*4882a593Smuzhiyun.. code-block:: c 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun struct exynos_spi_platdata platdata_spi0 = { 278*4882a593Smuzhiyun .periph_id = ... 279*4882a593Smuzhiyun .frequency = ... 280*4882a593Smuzhiyun .regs = ... 281*4882a593Smuzhiyun .deactivate_delay_us = ... 282*4882a593Smuzhiyun }; 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun U_BOOT_DEVICE(board_spi0) = { 285*4882a593Smuzhiyun .name = "exynos_spi", 286*4882a593Smuzhiyun .platdata = &platdata_spi0, 287*4882a593Smuzhiyun }; 288*4882a593Smuzhiyun 289*4882a593SmuzhiyunYou will unfortunately need to put the struct definition into a header file 290*4882a593Smuzhiyunin this case so that your board file can use it. 291*4882a593Smuzhiyun 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunAdd the device private data 294*4882a593Smuzhiyun--------------------------- 295*4882a593Smuzhiyun 296*4882a593SmuzhiyunMost devices have some private data which they use to keep track of things 297*4882a593Smuzhiyunwhile active. This is the run-time information and needs to be stored in 298*4882a593Smuzhiyuna structure. There is probably a structure in the driver that includes a 299*4882a593Smuzhiyun'struct spi_slave', so you can use that. 300*4882a593Smuzhiyun 301*4882a593Smuzhiyun.. code-block:: c 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun struct exynos_spi_slave { 304*4882a593Smuzhiyun struct spi_slave slave; 305*4882a593Smuzhiyun struct exynos_spi *regs; 306*4882a593Smuzhiyun unsigned int freq; /* Default frequency */ 307*4882a593Smuzhiyun unsigned int mode; 308*4882a593Smuzhiyun enum periph_id periph_id; /* Peripheral ID for this device */ 309*4882a593Smuzhiyun unsigned int fifo_size; 310*4882a593Smuzhiyun int skip_preamble; 311*4882a593Smuzhiyun struct spi_bus *bus; /* Pointer to our SPI bus info */ 312*4882a593Smuzhiyun ulong last_transaction_us; /* Time of last transaction end */ 313*4882a593Smuzhiyun }; 314*4882a593Smuzhiyun 315*4882a593Smuzhiyun 316*4882a593SmuzhiyunWe should rename this to make its purpose more obvious, and get rid of 317*4882a593Smuzhiyunthe slave structure, so we have: 318*4882a593Smuzhiyun 319*4882a593Smuzhiyun.. code-block:: c 320*4882a593Smuzhiyun 321*4882a593Smuzhiyun struct exynos_spi_priv { 322*4882a593Smuzhiyun struct exynos_spi *regs; 323*4882a593Smuzhiyun unsigned int freq; /* Default frequency */ 324*4882a593Smuzhiyun unsigned int mode; 325*4882a593Smuzhiyun enum periph_id periph_id; /* Peripheral ID for this device */ 326*4882a593Smuzhiyun unsigned int fifo_size; 327*4882a593Smuzhiyun int skip_preamble; 328*4882a593Smuzhiyun ulong last_transaction_us; /* Time of last transaction end */ 329*4882a593Smuzhiyun }; 330*4882a593Smuzhiyun 331*4882a593Smuzhiyun 332*4882a593SmuzhiyunDM can auto-allocate this also: 333*4882a593Smuzhiyun 334*4882a593Smuzhiyun.. code-block:: c 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun U_BOOT_DRIVER(spi_exynos) = { 337*4882a593Smuzhiyun ... 338*4882a593Smuzhiyun .priv_auto_alloc_size = sizeof(struct exynos_spi_priv), 339*4882a593Smuzhiyun 340*4882a593Smuzhiyun 341*4882a593SmuzhiyunNote that this is created before the probe method is called, and destroyed 342*4882a593Smuzhiyunafter the remove method is called. It will be zeroed when the probe 343*4882a593Smuzhiyunmethod is called. 344*4882a593Smuzhiyun 345*4882a593Smuzhiyun 346*4882a593SmuzhiyunAdd the probe() and remove() methods 347*4882a593Smuzhiyun------------------------------------ 348*4882a593Smuzhiyun 349*4882a593SmuzhiyunNote: It's a good idea to build repeatedly as you are working, to avoid a 350*4882a593Smuzhiyunhuge amount of work getting things compiling at the end. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunThe probe method is supposed to set up the hardware. U-Boot used to use 353*4882a593Smuzhiyunspi_setup_slave() to do this. So take a look at this function and see 354*4882a593Smuzhiyunwhat you can copy out to set things up. 355*4882a593Smuzhiyun 356*4882a593Smuzhiyun.. code-block:: c 357*4882a593Smuzhiyun 358*4882a593Smuzhiyun static int exynos_spi_probe(struct udevice *bus) 359*4882a593Smuzhiyun { 360*4882a593Smuzhiyun struct exynos_spi_platdata *plat = dev_get_platdata(bus); 361*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 362*4882a593Smuzhiyun 363*4882a593Smuzhiyun priv->regs = plat->regs; 364*4882a593Smuzhiyun if (plat->periph_id == PERIPH_ID_SPI1 || 365*4882a593Smuzhiyun plat->periph_id == PERIPH_ID_SPI2) 366*4882a593Smuzhiyun priv->fifo_size = 64; 367*4882a593Smuzhiyun else 368*4882a593Smuzhiyun priv->fifo_size = 256; 369*4882a593Smuzhiyun 370*4882a593Smuzhiyun priv->skip_preamble = 0; 371*4882a593Smuzhiyun priv->last_transaction_us = timer_get_us(); 372*4882a593Smuzhiyun priv->freq = plat->frequency; 373*4882a593Smuzhiyun priv->periph_id = plat->periph_id; 374*4882a593Smuzhiyun 375*4882a593Smuzhiyun return 0; 376*4882a593Smuzhiyun } 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunThis implementation doesn't actually touch the hardware, which is somewhat 379*4882a593Smuzhiyununusual for a driver. In this case we will do that when the device is 380*4882a593Smuzhiyunclaimed by something that wants to use the SPI bus. 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunFor remove we could shut down the clocks, but in this case there is 383*4882a593Smuzhiyunnothing to do. DM frees any memory that it allocated, so we can just 384*4882a593Smuzhiyunremove exynos_spi_remove() and its reference in U_BOOT_DRIVER. 385*4882a593Smuzhiyun 386*4882a593Smuzhiyun 387*4882a593SmuzhiyunImplement set_speed() 388*4882a593Smuzhiyun--------------------- 389*4882a593Smuzhiyun 390*4882a593SmuzhiyunThis should set up clocks so that the SPI bus is running at the right 391*4882a593Smuzhiyunspeed. With the old API spi_claim_bus() would normally do this and several 392*4882a593Smuzhiyunof the following functions, so let's look at that function: 393*4882a593Smuzhiyun 394*4882a593Smuzhiyun.. code-block:: c 395*4882a593Smuzhiyun 396*4882a593Smuzhiyun int spi_claim_bus(struct spi_slave *slave) 397*4882a593Smuzhiyun { 398*4882a593Smuzhiyun struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); 399*4882a593Smuzhiyun struct exynos_spi *regs = spi_slave->regs; 400*4882a593Smuzhiyun u32 reg = 0; 401*4882a593Smuzhiyun int ret; 402*4882a593Smuzhiyun 403*4882a593Smuzhiyun ret = set_spi_clk(spi_slave->periph_id, 404*4882a593Smuzhiyun spi_slave->freq); 405*4882a593Smuzhiyun if (ret < 0) { 406*4882a593Smuzhiyun debug("%s: Failed to setup spi clock\n", __func__); 407*4882a593Smuzhiyun return ret; 408*4882a593Smuzhiyun } 409*4882a593Smuzhiyun 410*4882a593Smuzhiyun exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE); 411*4882a593Smuzhiyun 412*4882a593Smuzhiyun spi_flush_fifo(slave); 413*4882a593Smuzhiyun 414*4882a593Smuzhiyun reg = readl(®s->ch_cfg); 415*4882a593Smuzhiyun reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L); 416*4882a593Smuzhiyun 417*4882a593Smuzhiyun if (spi_slave->mode & SPI_CPHA) 418*4882a593Smuzhiyun reg |= SPI_CH_CPHA_B; 419*4882a593Smuzhiyun 420*4882a593Smuzhiyun if (spi_slave->mode & SPI_CPOL) 421*4882a593Smuzhiyun reg |= SPI_CH_CPOL_L; 422*4882a593Smuzhiyun 423*4882a593Smuzhiyun writel(reg, ®s->ch_cfg); 424*4882a593Smuzhiyun writel(SPI_FB_DELAY_180, ®s->fb_clk); 425*4882a593Smuzhiyun 426*4882a593Smuzhiyun return 0; 427*4882a593Smuzhiyun } 428*4882a593Smuzhiyun 429*4882a593Smuzhiyun 430*4882a593SmuzhiyunIt sets up the speed, mode, pinmux, feedback delay and clears the FIFOs. 431*4882a593SmuzhiyunWith DM these will happen in separate methods. 432*4882a593Smuzhiyun 433*4882a593Smuzhiyun 434*4882a593SmuzhiyunHere is an example for the speed part: 435*4882a593Smuzhiyun 436*4882a593Smuzhiyun.. code-block:: c 437*4882a593Smuzhiyun 438*4882a593Smuzhiyun static int exynos_spi_set_speed(struct udevice *bus, uint speed) 439*4882a593Smuzhiyun { 440*4882a593Smuzhiyun struct exynos_spi_platdata *plat = bus->platdata; 441*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 442*4882a593Smuzhiyun int ret; 443*4882a593Smuzhiyun 444*4882a593Smuzhiyun if (speed > plat->frequency) 445*4882a593Smuzhiyun speed = plat->frequency; 446*4882a593Smuzhiyun ret = set_spi_clk(priv->periph_id, speed); 447*4882a593Smuzhiyun if (ret) 448*4882a593Smuzhiyun return ret; 449*4882a593Smuzhiyun priv->freq = speed; 450*4882a593Smuzhiyun debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq); 451*4882a593Smuzhiyun 452*4882a593Smuzhiyun return 0; 453*4882a593Smuzhiyun } 454*4882a593Smuzhiyun 455*4882a593Smuzhiyun 456*4882a593SmuzhiyunImplement set_mode() 457*4882a593Smuzhiyun-------------------- 458*4882a593Smuzhiyun 459*4882a593SmuzhiyunThis should adjust the SPI mode (polarity, etc.). Again this code probably 460*4882a593Smuzhiyuncomes from the old spi_claim_bus(). Here is an example: 461*4882a593Smuzhiyun 462*4882a593Smuzhiyun.. code-block:: c 463*4882a593Smuzhiyun 464*4882a593Smuzhiyun static int exynos_spi_set_mode(struct udevice *bus, uint mode) 465*4882a593Smuzhiyun { 466*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 467*4882a593Smuzhiyun uint32_t reg; 468*4882a593Smuzhiyun 469*4882a593Smuzhiyun reg = readl(&priv->regs->ch_cfg); 470*4882a593Smuzhiyun reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L); 471*4882a593Smuzhiyun 472*4882a593Smuzhiyun if (mode & SPI_CPHA) 473*4882a593Smuzhiyun reg |= SPI_CH_CPHA_B; 474*4882a593Smuzhiyun 475*4882a593Smuzhiyun if (mode & SPI_CPOL) 476*4882a593Smuzhiyun reg |= SPI_CH_CPOL_L; 477*4882a593Smuzhiyun 478*4882a593Smuzhiyun writel(reg, &priv->regs->ch_cfg); 479*4882a593Smuzhiyun priv->mode = mode; 480*4882a593Smuzhiyun debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode); 481*4882a593Smuzhiyun 482*4882a593Smuzhiyun return 0; 483*4882a593Smuzhiyun } 484*4882a593Smuzhiyun 485*4882a593Smuzhiyun 486*4882a593SmuzhiyunImplement claim_bus() 487*4882a593Smuzhiyun--------------------- 488*4882a593Smuzhiyun 489*4882a593SmuzhiyunThis is where a client wants to make use of the bus, so claims it first. 490*4882a593SmuzhiyunAt this point we need to make sure everything is set up ready for data 491*4882a593Smuzhiyuntransfer. Note that this function is wholly internal to the driver - at 492*4882a593Smuzhiyunpresent the SPI uclass never calls it. 493*4882a593Smuzhiyun 494*4882a593SmuzhiyunHere again we look at the old claim function and see some code that is 495*4882a593Smuzhiyunneeded. It is anything unrelated to speed and mode: 496*4882a593Smuzhiyun 497*4882a593Smuzhiyun.. code-block:: c 498*4882a593Smuzhiyun 499*4882a593Smuzhiyun static int exynos_spi_claim_bus(struct udevice *bus) 500*4882a593Smuzhiyun { 501*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 502*4882a593Smuzhiyun 503*4882a593Smuzhiyun exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE); 504*4882a593Smuzhiyun spi_flush_fifo(priv->regs); 505*4882a593Smuzhiyun 506*4882a593Smuzhiyun writel(SPI_FB_DELAY_180, &priv->regs->fb_clk); 507*4882a593Smuzhiyun 508*4882a593Smuzhiyun return 0; 509*4882a593Smuzhiyun } 510*4882a593Smuzhiyun 511*4882a593SmuzhiyunThe spi_flush_fifo() function is in the removed part of the code, so we 512*4882a593Smuzhiyunneed to expose it again (perhaps with an #endif before it and '#if 0' 513*4882a593Smuzhiyunafter it). It only needs access to priv->regs which is why we have 514*4882a593Smuzhiyunpassed that in: 515*4882a593Smuzhiyun 516*4882a593Smuzhiyun.. code-block:: c 517*4882a593Smuzhiyun 518*4882a593Smuzhiyun /** 519*4882a593Smuzhiyun * Flush spi tx, rx fifos and reset the SPI controller 520*4882a593Smuzhiyun * 521*4882a593Smuzhiyun * @param regs Pointer to SPI registers 522*4882a593Smuzhiyun */ 523*4882a593Smuzhiyun static void spi_flush_fifo(struct exynos_spi *regs) 524*4882a593Smuzhiyun { 525*4882a593Smuzhiyun clrsetbits_le32(®s->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST); 526*4882a593Smuzhiyun clrbits_le32(®s->ch_cfg, SPI_CH_RST); 527*4882a593Smuzhiyun setbits_le32(®s->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON); 528*4882a593Smuzhiyun } 529*4882a593Smuzhiyun 530*4882a593Smuzhiyun 531*4882a593SmuzhiyunImplement release_bus() 532*4882a593Smuzhiyun----------------------- 533*4882a593Smuzhiyun 534*4882a593SmuzhiyunThis releases the bus - in our example the old code in spi_release_bus() 535*4882a593Smuzhiyunis a call to spi_flush_fifo, so we add: 536*4882a593Smuzhiyun 537*4882a593Smuzhiyun.. code-block:: c 538*4882a593Smuzhiyun 539*4882a593Smuzhiyun static int exynos_spi_release_bus(struct udevice *bus) 540*4882a593Smuzhiyun { 541*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 542*4882a593Smuzhiyun 543*4882a593Smuzhiyun spi_flush_fifo(priv->regs); 544*4882a593Smuzhiyun 545*4882a593Smuzhiyun return 0; 546*4882a593Smuzhiyun } 547*4882a593Smuzhiyun 548*4882a593Smuzhiyun 549*4882a593SmuzhiyunImplement xfer() 550*4882a593Smuzhiyun---------------- 551*4882a593Smuzhiyun 552*4882a593SmuzhiyunThis is the final method that we need to create, and it is where all the 553*4882a593Smuzhiyunwork happens. The method parameters are the same as the old spi_xfer() with 554*4882a593Smuzhiyunthe addition of a 'struct udevice' so conversion is pretty easy. Start 555*4882a593Smuzhiyunby copying the contents of spi_xfer() to your new xfer() method and proceed 556*4882a593Smuzhiyunfrom there. 557*4882a593Smuzhiyun 558*4882a593SmuzhiyunIf (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an 559*4882a593Smuzhiyunactivate function, something like this: 560*4882a593Smuzhiyun 561*4882a593Smuzhiyun.. code-block:: c 562*4882a593Smuzhiyun 563*4882a593Smuzhiyun void spi_cs_activate(struct spi_slave *slave) 564*4882a593Smuzhiyun { 565*4882a593Smuzhiyun struct exynos_spi_slave *spi_slave = to_exynos_spi(slave); 566*4882a593Smuzhiyun 567*4882a593Smuzhiyun /* If it's too soon to do another transaction, wait */ 568*4882a593Smuzhiyun if (spi_slave->bus->deactivate_delay_us && 569*4882a593Smuzhiyun spi_slave->last_transaction_us) { 570*4882a593Smuzhiyun ulong delay_us; /* The delay completed so far */ 571*4882a593Smuzhiyun delay_us = timer_get_us() - spi_slave->last_transaction_us; 572*4882a593Smuzhiyun if (delay_us < spi_slave->bus->deactivate_delay_us) 573*4882a593Smuzhiyun udelay(spi_slave->bus->deactivate_delay_us - delay_us); 574*4882a593Smuzhiyun } 575*4882a593Smuzhiyun 576*4882a593Smuzhiyun clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT); 577*4882a593Smuzhiyun debug("Activate CS, bus %d\n", spi_slave->slave.bus); 578*4882a593Smuzhiyun spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE; 579*4882a593Smuzhiyun } 580*4882a593Smuzhiyun 581*4882a593SmuzhiyunThe new version looks like this: 582*4882a593Smuzhiyun 583*4882a593Smuzhiyun.. code-block:: c 584*4882a593Smuzhiyun 585*4882a593Smuzhiyun static void spi_cs_activate(struct udevice *dev) 586*4882a593Smuzhiyun { 587*4882a593Smuzhiyun struct udevice *bus = dev->parent; 588*4882a593Smuzhiyun struct exynos_spi_platdata *pdata = dev_get_platdata(bus); 589*4882a593Smuzhiyun struct exynos_spi_priv *priv = dev_get_priv(bus); 590*4882a593Smuzhiyun 591*4882a593Smuzhiyun /* If it's too soon to do another transaction, wait */ 592*4882a593Smuzhiyun if (pdata->deactivate_delay_us && 593*4882a593Smuzhiyun priv->last_transaction_us) { 594*4882a593Smuzhiyun ulong delay_us; /* The delay completed so far */ 595*4882a593Smuzhiyun delay_us = timer_get_us() - priv->last_transaction_us; 596*4882a593Smuzhiyun if (delay_us < pdata->deactivate_delay_us) 597*4882a593Smuzhiyun udelay(pdata->deactivate_delay_us - delay_us); 598*4882a593Smuzhiyun } 599*4882a593Smuzhiyun 600*4882a593Smuzhiyun clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT); 601*4882a593Smuzhiyun debug("Activate CS, bus '%s'\n", bus->name); 602*4882a593Smuzhiyun priv->skip_preamble = priv->mode & SPI_PREAMBLE; 603*4882a593Smuzhiyun } 604*4882a593Smuzhiyun 605*4882a593SmuzhiyunAll we have really done here is change the pointers and print the device name 606*4882a593Smuzhiyuninstead of the bus number. Other local static functions can be treated in 607*4882a593Smuzhiyunthe same way. 608*4882a593Smuzhiyun 609*4882a593Smuzhiyun 610*4882a593SmuzhiyunSet up the per-child data and child pre-probe function 611*4882a593Smuzhiyun------------------------------------------------------ 612*4882a593Smuzhiyun 613*4882a593SmuzhiyunTo minimise the pain and complexity of the SPI subsystem while the driver 614*4882a593Smuzhiyunmodel change-over is in place, struct spi_slave is used to reference a 615*4882a593SmuzhiyunSPI bus slave, even though that slave is actually a struct udevice. In fact 616*4882a593Smuzhiyunstruct spi_slave is the device's child data. We need to make sure this space 617*4882a593Smuzhiyunis available. It is possible to allocate more space that struct spi_slave 618*4882a593Smuzhiyunneeds, but this is the minimum. 619*4882a593Smuzhiyun 620*4882a593Smuzhiyun.. code-block:: c 621*4882a593Smuzhiyun 622*4882a593Smuzhiyun U_BOOT_DRIVER(exynos_spi) = { 623*4882a593Smuzhiyun ... 624*4882a593Smuzhiyun .per_child_auto_alloc_size = sizeof(struct spi_slave), 625*4882a593Smuzhiyun } 626*4882a593Smuzhiyun 627*4882a593Smuzhiyun 628*4882a593SmuzhiyunOptional: Set up cs_info() if you want it 629*4882a593Smuzhiyun----------------------------------------- 630*4882a593Smuzhiyun 631*4882a593SmuzhiyunSometimes it is useful to know whether a SPI chip select is valid, but this 632*4882a593Smuzhiyunis not obvious from outside the driver. In this case you can provide a 633*4882a593Smuzhiyunmethod for cs_info() to deal with this. If you don't provide it, then the 634*4882a593Smuzhiyundevice tree will be used to determine what chip selects are valid. 635*4882a593Smuzhiyun 636*4882a593SmuzhiyunReturn -ENODEV if the supplied chip select is invalid, or 0 if it is valid. 637*4882a593SmuzhiyunIf you don't provide the cs_info() method, 0 is assumed for all chip selects 638*4882a593Smuzhiyunthat do not appear in the device tree. 639*4882a593Smuzhiyun 640*4882a593Smuzhiyun 641*4882a593SmuzhiyunTest it 642*4882a593Smuzhiyun------- 643*4882a593Smuzhiyun 644*4882a593SmuzhiyunNow that you have the code written and it compiles, try testing it using 645*4882a593Smuzhiyunthe 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your 646*4882a593Smuzhiyunboard. 647*4882a593Smuzhiyun 648*4882a593Smuzhiyun 649*4882a593SmuzhiyunPrepare patches and send them to the mailing lists 650*4882a593Smuzhiyun-------------------------------------------------- 651*4882a593Smuzhiyun 652*4882a593SmuzhiyunYou can use 'tools/patman/patman' to prepare, check and send patches for 653*4882a593Smuzhiyunyour work. See tools/patman/README for details. 654*4882a593Smuzhiyun 655*4882a593SmuzhiyunA little note about SPI uclass features 656*4882a593Smuzhiyun--------------------------------------- 657*4882a593Smuzhiyun 658*4882a593SmuzhiyunThe SPI uclass keeps some information about each device 'dev' on the bus: 659*4882a593Smuzhiyun 660*4882a593Smuzhiyun struct dm_spi_slave_platdata: 661*4882a593Smuzhiyun This is device_get_parent_platdata(dev). 662*4882a593Smuzhiyun This is where the chip select number is stored, along with 663*4882a593Smuzhiyun the default bus speed and mode. It is automatically read 664*4882a593Smuzhiyun from the device tree in spi_child_post_bind(). It must not 665*4882a593Smuzhiyun be changed at run-time after being set up because platform 666*4882a593Smuzhiyun data is supposed to be immutable at run-time. 667*4882a593Smuzhiyun struct spi_slave: 668*4882a593Smuzhiyun This is device_get_parentdata(dev). 669*4882a593Smuzhiyun Already mentioned above. It holds run-time information about 670*4882a593Smuzhiyun the device. 671*4882a593Smuzhiyun 672*4882a593SmuzhiyunThere are also some SPI uclass methods that get called behind the scenes: 673*4882a593Smuzhiyun 674*4882a593Smuzhiyun spi_post_bind(): 675*4882a593Smuzhiyun Called when a new bus is bound. 676*4882a593Smuzhiyun This scans the device tree for devices on the bus, and binds 677*4882a593Smuzhiyun each one. This in turn causes spi_child_post_bind() to be 678*4882a593Smuzhiyun called for each, which reads the device tree information 679*4882a593Smuzhiyun into the parent (per-child) platform data. 680*4882a593Smuzhiyun spi_child_post_bind(): 681*4882a593Smuzhiyun Called when a new child is bound. 682*4882a593Smuzhiyun As mentioned above this reads the device tree information 683*4882a593Smuzhiyun into the per-child platform data 684*4882a593Smuzhiyun spi_child_pre_probe(): 685*4882a593Smuzhiyun Called before a new child is probed. 686*4882a593Smuzhiyun This sets up the mode and speed in struct spi_slave by 687*4882a593Smuzhiyun copying it from the parent's platform data for this child. 688*4882a593Smuzhiyun It also sets the 'dev' pointer, needed to permit passing 689*4882a593Smuzhiyun 'struct spi_slave' around the place without needing a 690*4882a593Smuzhiyun separate 'struct udevice' pointer. 691*4882a593Smuzhiyun 692*4882a593SmuzhiyunThe above housekeeping makes it easier to write your SPI driver. 693