atmel_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2007 Atmel Corporation
  4. */
  5. #include <common.h>
  6. #include <clk.h>
  7. #include <dm.h>
  8. #include <fdtdec.h>
  9. #include <spi.h>
  10. #include <malloc.h>
  11. #include <wait_bit.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/clk.h>
  14. #include <asm/arch/hardware.h>
  15. #ifdef CONFIG_DM_SPI
  16. #include <asm/arch/at91_spi.h>
  17. #endif
  18. #ifdef CONFIG_DM_GPIO
  19. #include <asm/gpio.h>
  20. #endif
  21. #include "atmel_spi.h"
  22. #ifndef CONFIG_DM_SPI
  23. static int spi_has_wdrbt(struct atmel_spi_slave *slave)
  24. {
  25. unsigned int ver;
  26. ver = spi_readl(slave, VERSION);
  27. return (ATMEL_SPI_VERSION_REV(ver) >= 0x210);
  28. }
  29. void spi_init()
  30. {
  31. }
  32. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  33. unsigned int max_hz, unsigned int mode)
  34. {
  35. struct atmel_spi_slave *as;
  36. unsigned int scbr;
  37. u32 csrx;
  38. void *regs;
  39. if (!spi_cs_is_valid(bus, cs))
  40. return NULL;
  41. switch (bus) {
  42. case 0:
  43. regs = (void *)ATMEL_BASE_SPI0;
  44. break;
  45. #ifdef ATMEL_BASE_SPI1
  46. case 1:
  47. regs = (void *)ATMEL_BASE_SPI1;
  48. break;
  49. #endif
  50. #ifdef ATMEL_BASE_SPI2
  51. case 2:
  52. regs = (void *)ATMEL_BASE_SPI2;
  53. break;
  54. #endif
  55. #ifdef ATMEL_BASE_SPI3
  56. case 3:
  57. regs = (void *)ATMEL_BASE_SPI3;
  58. break;
  59. #endif
  60. default:
  61. return NULL;
  62. }
  63. scbr = (get_spi_clk_rate(bus) + max_hz - 1) / max_hz;
  64. if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
  65. /* Too low max SCK rate */
  66. return NULL;
  67. if (scbr < 1)
  68. scbr = 1;
  69. csrx = ATMEL_SPI_CSRx_SCBR(scbr);
  70. csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
  71. if (!(mode & SPI_CPHA))
  72. csrx |= ATMEL_SPI_CSRx_NCPHA;
  73. if (mode & SPI_CPOL)
  74. csrx |= ATMEL_SPI_CSRx_CPOL;
  75. as = spi_alloc_slave(struct atmel_spi_slave, bus, cs);
  76. if (!as)
  77. return NULL;
  78. as->regs = regs;
  79. as->mr = ATMEL_SPI_MR_MSTR | ATMEL_SPI_MR_MODFDIS
  80. | ATMEL_SPI_MR_PCS(~(1 << cs) & 0xf);
  81. if (spi_has_wdrbt(as))
  82. as->mr |= ATMEL_SPI_MR_WDRBT;
  83. spi_writel(as, CSR(cs), csrx);
  84. return &as->slave;
  85. }
  86. void spi_free_slave(struct spi_slave *slave)
  87. {
  88. struct atmel_spi_slave *as = to_atmel_spi(slave);
  89. free(as);
  90. }
  91. int spi_claim_bus(struct spi_slave *slave)
  92. {
  93. struct atmel_spi_slave *as = to_atmel_spi(slave);
  94. /* Enable the SPI hardware */
  95. spi_writel(as, CR, ATMEL_SPI_CR_SPIEN);
  96. /*
  97. * Select the slave. This should set SCK to the correct
  98. * initial state, etc.
  99. */
  100. spi_writel(as, MR, as->mr);
  101. return 0;
  102. }
  103. void spi_release_bus(struct spi_slave *slave)
  104. {
  105. struct atmel_spi_slave *as = to_atmel_spi(slave);
  106. /* Disable the SPI hardware */
  107. spi_writel(as, CR, ATMEL_SPI_CR_SPIDIS);
  108. }
  109. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  110. const void *dout, void *din, unsigned long flags)
  111. {
  112. struct atmel_spi_slave *as = to_atmel_spi(slave);
  113. unsigned int len_tx;
  114. unsigned int len_rx;
  115. unsigned int len;
  116. u32 status;
  117. const u8 *txp = dout;
  118. u8 *rxp = din;
  119. u8 value;
  120. if (bitlen == 0)
  121. /* Finish any previously submitted transfers */
  122. goto out;
  123. /*
  124. * TODO: The controller can do non-multiple-of-8 bit
  125. * transfers, but this driver currently doesn't support it.
  126. *
  127. * It's also not clear how such transfers are supposed to be
  128. * represented as a stream of bytes...this is a limitation of
  129. * the current SPI interface.
  130. */
  131. if (bitlen % 8) {
  132. /* Errors always terminate an ongoing transfer */
  133. flags |= SPI_XFER_END;
  134. goto out;
  135. }
  136. len = bitlen / 8;
  137. /*
  138. * The controller can do automatic CS control, but it is
  139. * somewhat quirky, and it doesn't really buy us much anyway
  140. * in the context of U-Boot.
  141. */
  142. if (flags & SPI_XFER_BEGIN) {
  143. spi_cs_activate(slave);
  144. /*
  145. * sometimes the RDR is not empty when we get here,
  146. * in theory that should not happen, but it DOES happen.
  147. * Read it here to be on the safe side.
  148. * That also clears the OVRES flag. Required if the
  149. * following loop exits due to OVRES!
  150. */
  151. spi_readl(as, RDR);
  152. }
  153. for (len_tx = 0, len_rx = 0; len_rx < len; ) {
  154. status = spi_readl(as, SR);
  155. if (status & ATMEL_SPI_SR_OVRES)
  156. return -1;
  157. if (len_tx < len && (status & ATMEL_SPI_SR_TDRE)) {
  158. if (txp)
  159. value = *txp++;
  160. else
  161. value = 0;
  162. spi_writel(as, TDR, value);
  163. len_tx++;
  164. }
  165. if (status & ATMEL_SPI_SR_RDRF) {
  166. value = spi_readl(as, RDR);
  167. if (rxp)
  168. *rxp++ = value;
  169. len_rx++;
  170. }
  171. }
  172. out:
  173. if (flags & SPI_XFER_END) {
  174. /*
  175. * Wait until the transfer is completely done before
  176. * we deactivate CS.
  177. */
  178. do {
  179. status = spi_readl(as, SR);
  180. } while (!(status & ATMEL_SPI_SR_TXEMPTY));
  181. spi_cs_deactivate(slave);
  182. }
  183. return 0;
  184. }
  185. #else
  186. #define MAX_CS_COUNT 4
  187. struct atmel_spi_platdata {
  188. struct at91_spi *regs;
  189. };
  190. struct atmel_spi_priv {
  191. unsigned int freq; /* Default frequency */
  192. unsigned int mode;
  193. ulong bus_clk_rate;
  194. #ifdef CONFIG_DM_GPIO
  195. struct gpio_desc cs_gpios[MAX_CS_COUNT];
  196. #endif
  197. };
  198. static int atmel_spi_claim_bus(struct udevice *dev)
  199. {
  200. struct udevice *bus = dev_get_parent(dev);
  201. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  202. struct atmel_spi_priv *priv = dev_get_priv(bus);
  203. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  204. struct at91_spi *reg_base = bus_plat->regs;
  205. u32 cs = slave_plat->cs;
  206. u32 freq = priv->freq;
  207. u32 scbr, csrx, mode;
  208. scbr = (priv->bus_clk_rate + freq - 1) / freq;
  209. if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
  210. return -EINVAL;
  211. if (scbr < 1)
  212. scbr = 1;
  213. csrx = ATMEL_SPI_CSRx_SCBR(scbr);
  214. csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
  215. if (!(priv->mode & SPI_CPHA))
  216. csrx |= ATMEL_SPI_CSRx_NCPHA;
  217. if (priv->mode & SPI_CPOL)
  218. csrx |= ATMEL_SPI_CSRx_CPOL;
  219. writel(csrx, &reg_base->csr[cs]);
  220. mode = ATMEL_SPI_MR_MSTR |
  221. ATMEL_SPI_MR_MODFDIS |
  222. ATMEL_SPI_MR_WDRBT |
  223. ATMEL_SPI_MR_PCS(~(1 << cs));
  224. writel(mode, &reg_base->mr);
  225. writel(ATMEL_SPI_CR_SPIEN, &reg_base->cr);
  226. return 0;
  227. }
  228. static int atmel_spi_release_bus(struct udevice *dev)
  229. {
  230. struct udevice *bus = dev_get_parent(dev);
  231. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  232. writel(ATMEL_SPI_CR_SPIDIS, &bus_plat->regs->cr);
  233. return 0;
  234. }
  235. static void atmel_spi_cs_activate(struct udevice *dev)
  236. {
  237. #ifdef CONFIG_DM_GPIO
  238. struct udevice *bus = dev_get_parent(dev);
  239. struct atmel_spi_priv *priv = dev_get_priv(bus);
  240. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  241. u32 cs = slave_plat->cs;
  242. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  243. return;
  244. dm_gpio_set_value(&priv->cs_gpios[cs], 0);
  245. #endif
  246. }
  247. static void atmel_spi_cs_deactivate(struct udevice *dev)
  248. {
  249. #ifdef CONFIG_DM_GPIO
  250. struct udevice *bus = dev_get_parent(dev);
  251. struct atmel_spi_priv *priv = dev_get_priv(bus);
  252. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  253. u32 cs = slave_plat->cs;
  254. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  255. return;
  256. dm_gpio_set_value(&priv->cs_gpios[cs], 1);
  257. #endif
  258. }
  259. static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
  260. const void *dout, void *din, unsigned long flags)
  261. {
  262. struct udevice *bus = dev_get_parent(dev);
  263. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  264. struct at91_spi *reg_base = bus_plat->regs;
  265. u32 len_tx, len_rx, len;
  266. u32 status;
  267. const u8 *txp = dout;
  268. u8 *rxp = din;
  269. u8 value;
  270. if (bitlen == 0)
  271. goto out;
  272. /*
  273. * The controller can do non-multiple-of-8 bit
  274. * transfers, but this driver currently doesn't support it.
  275. *
  276. * It's also not clear how such transfers are supposed to be
  277. * represented as a stream of bytes...this is a limitation of
  278. * the current SPI interface.
  279. */
  280. if (bitlen % 8) {
  281. /* Errors always terminate an ongoing transfer */
  282. flags |= SPI_XFER_END;
  283. goto out;
  284. }
  285. len = bitlen / 8;
  286. /*
  287. * The controller can do automatic CS control, but it is
  288. * somewhat quirky, and it doesn't really buy us much anyway
  289. * in the context of U-Boot.
  290. */
  291. if (flags & SPI_XFER_BEGIN) {
  292. atmel_spi_cs_activate(dev);
  293. /*
  294. * sometimes the RDR is not empty when we get here,
  295. * in theory that should not happen, but it DOES happen.
  296. * Read it here to be on the safe side.
  297. * That also clears the OVRES flag. Required if the
  298. * following loop exits due to OVRES!
  299. */
  300. readl(&reg_base->rdr);
  301. }
  302. for (len_tx = 0, len_rx = 0; len_rx < len; ) {
  303. status = readl(&reg_base->sr);
  304. if (status & ATMEL_SPI_SR_OVRES)
  305. return -1;
  306. if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
  307. if (txp)
  308. value = *txp++;
  309. else
  310. value = 0;
  311. writel(value, &reg_base->tdr);
  312. len_tx++;
  313. }
  314. if (status & ATMEL_SPI_SR_RDRF) {
  315. value = readl(&reg_base->rdr);
  316. if (rxp)
  317. *rxp++ = value;
  318. len_rx++;
  319. }
  320. }
  321. out:
  322. if (flags & SPI_XFER_END) {
  323. /*
  324. * Wait until the transfer is completely done before
  325. * we deactivate CS.
  326. */
  327. wait_for_bit_le32(&reg_base->sr,
  328. ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
  329. atmel_spi_cs_deactivate(dev);
  330. }
  331. return 0;
  332. }
  333. static int atmel_spi_set_speed(struct udevice *bus, uint speed)
  334. {
  335. struct atmel_spi_priv *priv = dev_get_priv(bus);
  336. priv->freq = speed;
  337. return 0;
  338. }
  339. static int atmel_spi_set_mode(struct udevice *bus, uint mode)
  340. {
  341. struct atmel_spi_priv *priv = dev_get_priv(bus);
  342. priv->mode = mode;
  343. return 0;
  344. }
  345. static const struct dm_spi_ops atmel_spi_ops = {
  346. .claim_bus = atmel_spi_claim_bus,
  347. .release_bus = atmel_spi_release_bus,
  348. .xfer = atmel_spi_xfer,
  349. .set_speed = atmel_spi_set_speed,
  350. .set_mode = atmel_spi_set_mode,
  351. /*
  352. * cs_info is not needed, since we require all chip selects to be
  353. * in the device tree explicitly
  354. */
  355. };
  356. static int atmel_spi_enable_clk(struct udevice *bus)
  357. {
  358. struct atmel_spi_priv *priv = dev_get_priv(bus);
  359. struct clk clk;
  360. ulong clk_rate;
  361. int ret;
  362. ret = clk_get_by_index(bus, 0, &clk);
  363. if (ret)
  364. return -EINVAL;
  365. ret = clk_enable(&clk);
  366. if (ret)
  367. return ret;
  368. clk_rate = clk_get_rate(&clk);
  369. if (!clk_rate)
  370. return -EINVAL;
  371. priv->bus_clk_rate = clk_rate;
  372. clk_free(&clk);
  373. return 0;
  374. }
  375. static int atmel_spi_probe(struct udevice *bus)
  376. {
  377. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  378. int ret;
  379. ret = atmel_spi_enable_clk(bus);
  380. if (ret)
  381. return ret;
  382. bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
  383. #ifdef CONFIG_DM_GPIO
  384. struct atmel_spi_priv *priv = dev_get_priv(bus);
  385. int i;
  386. ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
  387. ARRAY_SIZE(priv->cs_gpios), 0);
  388. if (ret < 0) {
  389. pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
  390. return ret;
  391. }
  392. for(i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
  393. if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
  394. continue;
  395. dm_gpio_set_dir_flags(&priv->cs_gpios[i],
  396. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  397. }
  398. #endif
  399. writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
  400. return 0;
  401. }
  402. static const struct udevice_id atmel_spi_ids[] = {
  403. { .compatible = "atmel,at91rm9200-spi" },
  404. { }
  405. };
  406. U_BOOT_DRIVER(atmel_spi) = {
  407. .name = "atmel_spi",
  408. .id = UCLASS_SPI,
  409. .of_match = atmel_spi_ids,
  410. .ops = &atmel_spi_ops,
  411. .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
  412. .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
  413. .probe = atmel_spi_probe,
  414. };
  415. #endif