atmel_spi.c 10 KB

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