atmel_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  242. return;
  243. dm_gpio_set_value(&priv->cs_gpios[cs], 0);
  244. }
  245. static void atmel_spi_cs_deactivate(struct udevice *dev)
  246. {
  247. struct udevice *bus = dev_get_parent(dev);
  248. struct atmel_spi_priv *priv = dev_get_priv(bus);
  249. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  250. u32 cs = slave_plat->cs;
  251. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  252. return;
  253. dm_gpio_set_value(&priv->cs_gpios[cs], 1);
  254. }
  255. static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
  256. const void *dout, void *din, unsigned long flags)
  257. {
  258. struct udevice *bus = dev_get_parent(dev);
  259. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  260. struct at91_spi *reg_base = bus_plat->regs;
  261. u32 len_tx, len_rx, len;
  262. u32 status;
  263. const u8 *txp = dout;
  264. u8 *rxp = din;
  265. u8 value;
  266. if (bitlen == 0)
  267. goto out;
  268. /*
  269. * The controller can do non-multiple-of-8 bit
  270. * transfers, but this driver currently doesn't support it.
  271. *
  272. * It's also not clear how such transfers are supposed to be
  273. * represented as a stream of bytes...this is a limitation of
  274. * the current SPI interface.
  275. */
  276. if (bitlen % 8) {
  277. /* Errors always terminate an ongoing transfer */
  278. flags |= SPI_XFER_END;
  279. goto out;
  280. }
  281. len = bitlen / 8;
  282. /*
  283. * The controller can do automatic CS control, but it is
  284. * somewhat quirky, and it doesn't really buy us much anyway
  285. * in the context of U-Boot.
  286. */
  287. if (flags & SPI_XFER_BEGIN) {
  288. atmel_spi_cs_activate(dev);
  289. /*
  290. * sometimes the RDR is not empty when we get here,
  291. * in theory that should not happen, but it DOES happen.
  292. * Read it here to be on the safe side.
  293. * That also clears the OVRES flag. Required if the
  294. * following loop exits due to OVRES!
  295. */
  296. readl(&reg_base->rdr);
  297. }
  298. for (len_tx = 0, len_rx = 0; len_rx < len; ) {
  299. status = readl(&reg_base->sr);
  300. if (status & ATMEL_SPI_SR_OVRES)
  301. return -1;
  302. if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
  303. if (txp)
  304. value = *txp++;
  305. else
  306. value = 0;
  307. writel(value, &reg_base->tdr);
  308. len_tx++;
  309. }
  310. if (status & ATMEL_SPI_SR_RDRF) {
  311. value = readl(&reg_base->rdr);
  312. if (rxp)
  313. *rxp++ = value;
  314. len_rx++;
  315. }
  316. }
  317. out:
  318. if (flags & SPI_XFER_END) {
  319. /*
  320. * Wait until the transfer is completely done before
  321. * we deactivate CS.
  322. */
  323. wait_for_bit_le32(&reg_base->sr,
  324. ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
  325. atmel_spi_cs_deactivate(dev);
  326. }
  327. return 0;
  328. }
  329. static int atmel_spi_set_speed(struct udevice *bus, uint speed)
  330. {
  331. struct atmel_spi_priv *priv = dev_get_priv(bus);
  332. priv->freq = speed;
  333. return 0;
  334. }
  335. static int atmel_spi_set_mode(struct udevice *bus, uint mode)
  336. {
  337. struct atmel_spi_priv *priv = dev_get_priv(bus);
  338. priv->mode = mode;
  339. return 0;
  340. }
  341. static const struct dm_spi_ops atmel_spi_ops = {
  342. .claim_bus = atmel_spi_claim_bus,
  343. .release_bus = atmel_spi_release_bus,
  344. .xfer = atmel_spi_xfer,
  345. .set_speed = atmel_spi_set_speed,
  346. .set_mode = atmel_spi_set_mode,
  347. /*
  348. * cs_info is not needed, since we require all chip selects to be
  349. * in the device tree explicitly
  350. */
  351. };
  352. static int atmel_spi_enable_clk(struct udevice *bus)
  353. {
  354. struct atmel_spi_priv *priv = dev_get_priv(bus);
  355. struct clk clk;
  356. ulong clk_rate;
  357. int ret;
  358. ret = clk_get_by_index(bus, 0, &clk);
  359. if (ret)
  360. return -EINVAL;
  361. ret = clk_enable(&clk);
  362. if (ret)
  363. return ret;
  364. clk_rate = clk_get_rate(&clk);
  365. if (!clk_rate)
  366. return -EINVAL;
  367. priv->bus_clk_rate = clk_rate;
  368. clk_free(&clk);
  369. return 0;
  370. }
  371. static int atmel_spi_probe(struct udevice *bus)
  372. {
  373. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  374. struct atmel_spi_priv *priv = dev_get_priv(bus);
  375. int i, ret;
  376. ret = atmel_spi_enable_clk(bus);
  377. if (ret)
  378. return ret;
  379. bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
  380. ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
  381. ARRAY_SIZE(priv->cs_gpios), 0);
  382. if (ret < 0) {
  383. pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
  384. return ret;
  385. }
  386. for(i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
  387. if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
  388. continue;
  389. dm_gpio_set_dir_flags(&priv->cs_gpios[i],
  390. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  391. }
  392. writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
  393. return 0;
  394. }
  395. static const struct udevice_id atmel_spi_ids[] = {
  396. { .compatible = "atmel,at91rm9200-spi" },
  397. { }
  398. };
  399. U_BOOT_DRIVER(atmel_spi) = {
  400. .name = "atmel_spi",
  401. .id = UCLASS_SPI,
  402. .of_match = atmel_spi_ids,
  403. .ops = &atmel_spi_ops,
  404. .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
  405. .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
  406. .probe = atmel_spi_probe,
  407. };
  408. #endif