Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/uclass-internal.h> |
| 15 | #include <dm/root.h> |
| 16 | #include <dm/lists.h> |
| 17 | #include <dm/util.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | static int spi_set_speed_mode(struct udevice *bus, int speed, int mode) |
| 22 | { |
| 23 | struct dm_spi_ops *ops; |
| 24 | int ret; |
| 25 | |
| 26 | ops = spi_get_ops(bus); |
| 27 | if (ops->set_speed) |
| 28 | ret = ops->set_speed(bus, speed); |
| 29 | else |
| 30 | ret = -EINVAL; |
| 31 | if (ret) { |
| 32 | printf("Cannot set speed (err=%d)\n", ret); |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | if (ops->set_mode) |
| 37 | ret = ops->set_mode(bus, mode); |
| 38 | else |
| 39 | ret = -EINVAL; |
| 40 | if (ret) { |
| 41 | printf("Cannot set mode (err=%d)\n", ret); |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int spi_claim_bus(struct spi_slave *slave) |
| 49 | { |
| 50 | struct udevice *dev = slave->dev; |
| 51 | struct udevice *bus = dev->parent; |
| 52 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 53 | struct dm_spi_bus *spi = bus->uclass_priv; |
| 54 | int speed; |
| 55 | int ret; |
| 56 | |
| 57 | speed = slave->max_hz; |
| 58 | if (spi->max_hz) { |
| 59 | if (speed) |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 60 | speed = min(speed, (int)spi->max_hz); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 61 | else |
| 62 | speed = spi->max_hz; |
| 63 | } |
| 64 | if (!speed) |
| 65 | speed = 100000; |
| 66 | ret = spi_set_speed_mode(bus, speed, slave->mode); |
| 67 | if (ret) |
| 68 | return ret; |
| 69 | |
| 70 | return ops->claim_bus ? ops->claim_bus(bus) : 0; |
| 71 | } |
| 72 | |
| 73 | void spi_release_bus(struct spi_slave *slave) |
| 74 | { |
| 75 | struct udevice *dev = slave->dev; |
| 76 | struct udevice *bus = dev->parent; |
| 77 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 78 | |
| 79 | if (ops->release_bus) |
| 80 | ops->release_bus(bus); |
| 81 | } |
| 82 | |
| 83 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 84 | const void *dout, void *din, unsigned long flags) |
| 85 | { |
| 86 | struct udevice *dev = slave->dev; |
| 87 | struct udevice *bus = dev->parent; |
| 88 | |
| 89 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 90 | return -EOPNOTSUPP; |
| 91 | |
| 92 | return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags); |
| 93 | } |
| 94 | |
| 95 | int spi_post_bind(struct udevice *dev) |
| 96 | { |
| 97 | /* Scan the bus for devices */ |
| 98 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
| 99 | } |
| 100 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 101 | int spi_child_post_bind(struct udevice *dev) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 102 | { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 103 | struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 104 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 105 | if (dev->of_offset == -1) |
| 106 | return 0; |
| 107 | |
| 108 | return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat); |
| 109 | } |
| 110 | |
| 111 | int spi_post_probe(struct udevice *bus) |
| 112 | { |
| 113 | struct dm_spi_bus *spi = bus->uclass_priv; |
| 114 | |
| 115 | spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset, |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 116 | "spi-max-frequency", 0); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 121 | int spi_child_pre_probe(struct udevice *dev) |
| 122 | { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 123 | struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 124 | struct spi_slave *slave = dev_get_parentdata(dev); |
| 125 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 126 | /* |
| 127 | * This is needed because we pass struct spi_slave around the place |
| 128 | * instead slave->dev (a struct udevice). So we have to have some |
| 129 | * way to access the slave udevice given struct spi_slave. Once we |
| 130 | * change the SPI API to use udevice instead of spi_slave, we can |
| 131 | * drop this. |
| 132 | */ |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 133 | slave->dev = dev; |
| 134 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 135 | slave->max_hz = plat->max_hz; |
| 136 | slave->mode = plat->mode; |
| 137 | |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 141 | int spi_chip_select(struct udevice *dev) |
| 142 | { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 143 | struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 144 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 145 | return plat ? plat->cs : -ENOENT; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Simon Glass | ff56bba | 2014-11-11 10:46:22 -0700 | [diff] [blame] | 148 | int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 149 | { |
| 150 | struct udevice *dev; |
| 151 | |
| 152 | for (device_find_first_child(bus, &dev); dev; |
| 153 | device_find_next_child(&dev)) { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 154 | struct dm_spi_slave_platdata *plat; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 155 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 156 | plat = dev_get_parent_platdata(dev); |
| 157 | debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs); |
| 158 | if (plat->cs == cs) { |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 159 | *devp = dev; |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return -ENODEV; |
| 165 | } |
| 166 | |
| 167 | int spi_cs_is_valid(unsigned int busnum, unsigned int cs) |
| 168 | { |
| 169 | struct spi_cs_info info; |
| 170 | struct udevice *bus; |
| 171 | int ret; |
| 172 | |
| 173 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus); |
| 174 | if (ret) { |
| 175 | debug("%s: No bus %d\n", __func__, busnum); |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | return spi_cs_info(bus, cs, &info); |
| 180 | } |
| 181 | |
| 182 | int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) |
| 183 | { |
| 184 | struct spi_cs_info local_info; |
| 185 | struct dm_spi_ops *ops; |
| 186 | int ret; |
| 187 | |
| 188 | if (!info) |
| 189 | info = &local_info; |
| 190 | |
| 191 | /* If there is a device attached, return it */ |
| 192 | info->dev = NULL; |
| 193 | ret = spi_find_chip_select(bus, cs, &info->dev); |
| 194 | if (!ret) |
| 195 | return 0; |
| 196 | |
| 197 | /* |
| 198 | * Otherwise ask the driver. For the moment we don't have CS info. |
| 199 | * When we do we could provide the driver with a helper function |
| 200 | * to figure out what chip selects are valid, or just handle the |
| 201 | * request. |
| 202 | */ |
| 203 | ops = spi_get_ops(bus); |
| 204 | if (ops->cs_info) |
| 205 | return ops->cs_info(bus, cs, info); |
| 206 | |
| 207 | /* |
| 208 | * We could assume there is at least one valid chip select, but best |
| 209 | * to be sure and return an error in this case. The driver didn't |
| 210 | * care enough to tell us. |
| 211 | */ |
| 212 | return -ENODEV; |
| 213 | } |
| 214 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 215 | int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, |
| 216 | struct udevice **devp) |
| 217 | { |
| 218 | struct udevice *bus, *dev; |
| 219 | int ret; |
| 220 | |
| 221 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus); |
| 222 | if (ret) { |
| 223 | debug("%s: No bus %d\n", __func__, busnum); |
| 224 | return ret; |
| 225 | } |
| 226 | ret = spi_find_chip_select(bus, cs, &dev); |
| 227 | if (ret) { |
| 228 | debug("%s: No cs %d\n", __func__, cs); |
| 229 | return ret; |
| 230 | } |
| 231 | *busp = bus; |
| 232 | *devp = dev; |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode, |
| 238 | const char *drv_name, const char *dev_name, |
| 239 | struct udevice **busp, struct spi_slave **devp) |
| 240 | { |
| 241 | struct udevice *bus, *dev; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 242 | bool created = false; |
| 243 | int ret; |
| 244 | |
| 245 | ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); |
| 246 | if (ret) { |
| 247 | printf("Invalid bus %d (err=%d)\n", busnum, ret); |
| 248 | return ret; |
| 249 | } |
| 250 | ret = spi_find_chip_select(bus, cs, &dev); |
| 251 | |
| 252 | /* |
| 253 | * If there is no such device, create one automatically. This means |
| 254 | * that we don't need a device tree node or platform data for the |
| 255 | * SPI flash chip - we will bind to the correct driver. |
| 256 | */ |
| 257 | if (ret == -ENODEV && drv_name) { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 258 | struct dm_spi_slave_platdata *plat; |
| 259 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 260 | debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n", |
| 261 | __func__, dev_name, busnum, cs, drv_name); |
Simon Glass | 6b18656 | 2014-11-11 10:46:23 -0700 | [diff] [blame] | 262 | ret = device_bind_driver(bus, drv_name, dev_name, &dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 265 | plat = dev_get_parent_platdata(dev); |
| 266 | plat->cs = cs; |
| 267 | plat->max_hz = speed; |
| 268 | plat->mode = mode; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 269 | created = true; |
| 270 | } else if (ret) { |
| 271 | printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs, |
| 272 | ret); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | if (!device_active(dev)) { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 277 | struct spi_slave *slave; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 278 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 279 | ret = device_probe(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 280 | if (ret) |
| 281 | goto err; |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 282 | slave = dev_get_parentdata(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 283 | slave->dev = dev; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | ret = spi_set_speed_mode(bus, speed, mode); |
| 287 | if (ret) |
| 288 | goto err; |
| 289 | |
| 290 | *busp = bus; |
| 291 | *devp = dev_get_parentdata(dev); |
| 292 | debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp); |
| 293 | |
| 294 | return 0; |
| 295 | |
| 296 | err: |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 297 | debug("%s: Error path, credted=%d, device '%s'\n", __func__, |
| 298 | created, dev->name); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 299 | if (created) { |
| 300 | device_remove(dev); |
| 301 | device_unbind(dev); |
| 302 | } |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | /* Compatibility function - to be removed */ |
| 308 | struct spi_slave *spi_setup_slave_fdt(const void *blob, int node, |
| 309 | int bus_node) |
| 310 | { |
| 311 | struct udevice *bus, *dev; |
| 312 | int ret; |
| 313 | |
| 314 | ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus); |
| 315 | if (ret) |
| 316 | return NULL; |
| 317 | ret = device_get_child_by_of_offset(bus, node, &dev); |
| 318 | if (ret) |
| 319 | return NULL; |
| 320 | return dev_get_parentdata(dev); |
| 321 | } |
| 322 | |
| 323 | /* Compatibility function - to be removed */ |
| 324 | struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, |
| 325 | unsigned int speed, unsigned int mode) |
| 326 | { |
| 327 | struct spi_slave *slave; |
| 328 | struct udevice *dev; |
| 329 | int ret; |
| 330 | |
| 331 | ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev, |
| 332 | &slave); |
| 333 | if (ret) |
| 334 | return NULL; |
| 335 | |
| 336 | return slave; |
| 337 | } |
| 338 | |
| 339 | void spi_free_slave(struct spi_slave *slave) |
| 340 | { |
| 341 | device_remove(slave->dev); |
| 342 | slave->dev = NULL; |
| 343 | } |
| 344 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 345 | int spi_slave_ofdata_to_platdata(const void *blob, int node, |
| 346 | struct dm_spi_slave_platdata *plat) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 347 | { |
| 348 | int mode = 0; |
| 349 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 350 | plat->cs = fdtdec_get_int(blob, node, "reg", -1); |
| 351 | plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 352 | if (fdtdec_get_bool(blob, node, "spi-cpol")) |
| 353 | mode |= SPI_CPOL; |
| 354 | if (fdtdec_get_bool(blob, node, "spi-cpha")) |
| 355 | mode |= SPI_CPHA; |
| 356 | if (fdtdec_get_bool(blob, node, "spi-cs-high")) |
| 357 | mode |= SPI_CS_HIGH; |
| 358 | if (fdtdec_get_bool(blob, node, "spi-half-duplex")) |
| 359 | mode |= SPI_PREAMBLE; |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 360 | plat->mode = mode; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | UCLASS_DRIVER(spi) = { |
| 366 | .id = UCLASS_SPI, |
| 367 | .name = "spi", |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 368 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 369 | .post_bind = spi_post_bind, |
| 370 | .post_probe = spi_post_probe, |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 371 | .child_pre_probe = spi_child_pre_probe, |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 372 | .per_device_auto_alloc_size = sizeof(struct dm_spi_bus), |
Simon Glass | 19a25f6 | 2015-01-25 08:27:07 -0700 | [diff] [blame] | 373 | .per_child_auto_alloc_size = sizeof(struct spi_slave), |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 374 | .per_child_platdata_auto_alloc_size = |
| 375 | sizeof(struct dm_spi_slave_platdata), |
| 376 | .child_post_bind = spi_child_post_bind, |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 377 | }; |
| 378 | |
| 379 | UCLASS_DRIVER(spi_generic) = { |
| 380 | .id = UCLASS_SPI_GENERIC, |
| 381 | .name = "spi_generic", |
| 382 | }; |
| 383 | |
| 384 | U_BOOT_DRIVER(spi_generic_drv) = { |
| 385 | .name = "spi_generic_drv", |
| 386 | .id = UCLASS_SPI_GENERIC, |
| 387 | }; |