davinci_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  4. *
  5. * Driver for SPI controller on DaVinci. Based on atmel_spi.c
  6. * by Atmel Corporation
  7. *
  8. * Copyright (C) 2007 Atmel Corporation
  9. */
  10. #include <common.h>
  11. #include <spi.h>
  12. #include <malloc.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/hardware.h>
  15. #include <dm.h>
  16. /* SPIGCR0 */
  17. #define SPIGCR0_SPIENA_MASK 0x1
  18. #define SPIGCR0_SPIRST_MASK 0x0
  19. /* SPIGCR0 */
  20. #define SPIGCR1_CLKMOD_MASK BIT(1)
  21. #define SPIGCR1_MASTER_MASK BIT(0)
  22. #define SPIGCR1_SPIENA_MASK BIT(24)
  23. /* SPIPC0 */
  24. #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
  25. #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
  26. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  27. #define SPIPC0_EN0FUN_MASK BIT(0)
  28. /* SPIFMT0 */
  29. #define SPIFMT_SHIFTDIR_SHIFT 20
  30. #define SPIFMT_POLARITY_SHIFT 17
  31. #define SPIFMT_PHASE_SHIFT 16
  32. #define SPIFMT_PRESCALE_SHIFT 8
  33. /* SPIDAT1 */
  34. #define SPIDAT1_CSHOLD_SHIFT 28
  35. #define SPIDAT1_CSNR_SHIFT 16
  36. /* SPIDELAY */
  37. #define SPI_C2TDELAY_SHIFT 24
  38. #define SPI_T2CDELAY_SHIFT 16
  39. /* SPIBUF */
  40. #define SPIBUF_RXEMPTY_MASK BIT(31)
  41. #define SPIBUF_TXFULL_MASK BIT(29)
  42. /* SPIDEF */
  43. #define SPIDEF_CSDEF0_MASK BIT(0)
  44. #ifndef CONFIG_DM_SPI
  45. #define SPI0_BUS 0
  46. #define SPI0_BASE CONFIG_SYS_SPI_BASE
  47. /*
  48. * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
  49. * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
  50. * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
  51. */
  52. #ifndef CONFIG_SYS_SPI0
  53. #define SPI0_NUM_CS 1
  54. #else
  55. #define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS
  56. #endif
  57. /*
  58. * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
  59. * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
  60. */
  61. #ifdef CONFIG_SYS_SPI1
  62. #define SPI1_BUS 1
  63. #define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS
  64. #define SPI1_BASE CONFIG_SYS_SPI1_BASE
  65. #endif
  66. /*
  67. * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
  68. * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
  69. */
  70. #ifdef CONFIG_SYS_SPI2
  71. #define SPI2_BUS 2
  72. #define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS
  73. #define SPI2_BASE CONFIG_SYS_SPI2_BASE
  74. #endif
  75. #endif
  76. DECLARE_GLOBAL_DATA_PTR;
  77. /* davinci spi register set */
  78. struct davinci_spi_regs {
  79. dv_reg gcr0; /* 0x00 */
  80. dv_reg gcr1; /* 0x04 */
  81. dv_reg int0; /* 0x08 */
  82. dv_reg lvl; /* 0x0c */
  83. dv_reg flg; /* 0x10 */
  84. dv_reg pc0; /* 0x14 */
  85. dv_reg pc1; /* 0x18 */
  86. dv_reg pc2; /* 0x1c */
  87. dv_reg pc3; /* 0x20 */
  88. dv_reg pc4; /* 0x24 */
  89. dv_reg pc5; /* 0x28 */
  90. dv_reg rsvd[3];
  91. dv_reg dat0; /* 0x38 */
  92. dv_reg dat1; /* 0x3c */
  93. dv_reg buf; /* 0x40 */
  94. dv_reg emu; /* 0x44 */
  95. dv_reg delay; /* 0x48 */
  96. dv_reg def; /* 0x4c */
  97. dv_reg fmt0; /* 0x50 */
  98. dv_reg fmt1; /* 0x54 */
  99. dv_reg fmt2; /* 0x58 */
  100. dv_reg fmt3; /* 0x5c */
  101. dv_reg intvec0; /* 0x60 */
  102. dv_reg intvec1; /* 0x64 */
  103. };
  104. /* davinci spi slave */
  105. struct davinci_spi_slave {
  106. #ifndef CONFIG_DM_SPI
  107. struct spi_slave slave;
  108. #endif
  109. struct davinci_spi_regs *regs;
  110. unsigned int freq; /* current SPI bus frequency */
  111. unsigned int mode; /* current SPI mode used */
  112. u8 num_cs; /* total no. of CS available */
  113. u8 cur_cs; /* CS of current slave */
  114. bool half_duplex; /* true, if master is half-duplex only */
  115. };
  116. /*
  117. * This functions needs to act like a macro to avoid pipeline reloads in the
  118. * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
  119. * appears to be zero bytes (da830).
  120. */
  121. __attribute__((always_inline))
  122. static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
  123. {
  124. u32 buf_reg_val;
  125. /* send out data */
  126. writel(data, &ds->regs->dat1);
  127. /* wait for the data to clock in/out */
  128. while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
  129. ;
  130. return buf_reg_val;
  131. }
  132. static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
  133. u8 *rxp, unsigned long flags)
  134. {
  135. unsigned int data1_reg_val;
  136. /* enable CS hold, CS[n] and clear the data bits */
  137. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  138. (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
  139. /* wait till TXFULL is deasserted */
  140. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  141. ;
  142. /* preload the TX buffer to avoid clock starvation */
  143. writel(data1_reg_val, &ds->regs->dat1);
  144. /* keep reading 1 byte until only 1 byte left */
  145. while ((len--) > 1)
  146. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
  147. /* clear CS hold when we reach the end */
  148. if (flags & SPI_XFER_END)
  149. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  150. /* read the last byte */
  151. *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
  152. return 0;
  153. }
  154. static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
  155. const u8 *txp, unsigned long flags)
  156. {
  157. unsigned int data1_reg_val;
  158. /* enable CS hold and clear the data bits */
  159. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  160. (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
  161. /* wait till TXFULL is deasserted */
  162. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  163. ;
  164. /* preload the TX buffer to avoid clock starvation */
  165. if (len > 2) {
  166. writel(data1_reg_val | *txp++, &ds->regs->dat1);
  167. len--;
  168. }
  169. /* keep writing 1 byte until only 1 byte left */
  170. while ((len--) > 1)
  171. davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  172. /* clear CS hold when we reach the end */
  173. if (flags & SPI_XFER_END)
  174. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  175. /* write the last byte */
  176. davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  177. return 0;
  178. }
  179. static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
  180. int len, u8 *rxp, const u8 *txp,
  181. unsigned long flags)
  182. {
  183. unsigned int data1_reg_val;
  184. /* enable CS hold and clear the data bits */
  185. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  186. (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
  187. /* wait till TXFULL is deasserted */
  188. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  189. ;
  190. /* keep reading and writing 1 byte until only 1 byte left */
  191. while ((len--) > 1)
  192. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  193. /* clear CS hold when we reach the end */
  194. if (flags & SPI_XFER_END)
  195. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  196. /* read and write the last byte */
  197. *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  198. return 0;
  199. }
  200. static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
  201. {
  202. unsigned int mode = 0, scalar;
  203. /* Enable the SPI hardware */
  204. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  205. udelay(1000);
  206. writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
  207. /* Set master mode, powered up and not activated */
  208. writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
  209. /* CS, CLK, SIMO and SOMI are functional pins */
  210. writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
  211. SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
  212. /* setup format */
  213. scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
  214. /*
  215. * Use following format:
  216. * character length = 8,
  217. * MSB shifted out first
  218. */
  219. if (ds->mode & SPI_CPOL)
  220. mode |= SPI_CPOL;
  221. if (!(ds->mode & SPI_CPHA))
  222. mode |= SPI_CPHA;
  223. writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
  224. (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
  225. /*
  226. * Including a minor delay. No science here. Should be good even with
  227. * no delay
  228. */
  229. writel((50 << SPI_C2TDELAY_SHIFT) |
  230. (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
  231. /* default chip select register */
  232. writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
  233. /* no interrupts */
  234. writel(0, &ds->regs->int0);
  235. writel(0, &ds->regs->lvl);
  236. /* enable SPI */
  237. writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
  238. return 0;
  239. }
  240. static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
  241. {
  242. /* Disable the SPI hardware */
  243. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  244. return 0;
  245. }
  246. static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
  247. unsigned int bitlen, const void *dout, void *din,
  248. unsigned long flags)
  249. {
  250. unsigned int len;
  251. if (bitlen == 0)
  252. /* Finish any previously submitted transfers */
  253. goto out;
  254. /*
  255. * It's not clear how non-8-bit-aligned transfers are supposed to be
  256. * represented as a stream of bytes...this is a limitation of
  257. * the current SPI interface - here we terminate on receiving such a
  258. * transfer request.
  259. */
  260. if (bitlen % 8) {
  261. /* Errors always terminate an ongoing transfer */
  262. flags |= SPI_XFER_END;
  263. goto out;
  264. }
  265. len = bitlen / 8;
  266. if (!dout)
  267. return davinci_spi_read(ds, len, din, flags);
  268. if (!din)
  269. return davinci_spi_write(ds, len, dout, flags);
  270. if (!ds->half_duplex)
  271. return davinci_spi_read_write(ds, len, din, dout, flags);
  272. printf("SPI full duplex not supported\n");
  273. flags |= SPI_XFER_END;
  274. out:
  275. if (flags & SPI_XFER_END) {
  276. u8 dummy = 0;
  277. davinci_spi_write(ds, 1, &dummy, flags);
  278. }
  279. return 0;
  280. }
  281. #ifndef CONFIG_DM_SPI
  282. static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
  283. {
  284. return container_of(slave, struct davinci_spi_slave, slave);
  285. }
  286. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  287. {
  288. int ret = 0;
  289. switch (bus) {
  290. case SPI0_BUS:
  291. if (cs < SPI0_NUM_CS)
  292. ret = 1;
  293. break;
  294. #ifdef CONFIG_SYS_SPI1
  295. case SPI1_BUS:
  296. if (cs < SPI1_NUM_CS)
  297. ret = 1;
  298. break;
  299. #endif
  300. #ifdef CONFIG_SYS_SPI2
  301. case SPI2_BUS:
  302. if (cs < SPI2_NUM_CS)
  303. ret = 1;
  304. break;
  305. #endif
  306. default:
  307. /* Invalid bus number. Do nothing */
  308. break;
  309. }
  310. return ret;
  311. }
  312. void spi_cs_activate(struct spi_slave *slave)
  313. {
  314. /* do nothing */
  315. }
  316. void spi_cs_deactivate(struct spi_slave *slave)
  317. {
  318. /* do nothing */
  319. }
  320. void spi_init(void)
  321. {
  322. /* do nothing */
  323. }
  324. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  325. unsigned int max_hz, unsigned int mode)
  326. {
  327. struct davinci_spi_slave *ds;
  328. if (!spi_cs_is_valid(bus, cs))
  329. return NULL;
  330. ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
  331. if (!ds)
  332. return NULL;
  333. switch (bus) {
  334. case SPI0_BUS:
  335. ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
  336. break;
  337. #ifdef CONFIG_SYS_SPI1
  338. case SPI1_BUS:
  339. ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
  340. break;
  341. #endif
  342. #ifdef CONFIG_SYS_SPI2
  343. case SPI2_BUS:
  344. ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
  345. break;
  346. #endif
  347. default: /* Invalid bus number */
  348. return NULL;
  349. }
  350. ds->freq = max_hz;
  351. ds->mode = mode;
  352. return &ds->slave;
  353. }
  354. void spi_free_slave(struct spi_slave *slave)
  355. {
  356. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  357. free(ds);
  358. }
  359. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  360. const void *dout, void *din, unsigned long flags)
  361. {
  362. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  363. ds->cur_cs = slave->cs;
  364. return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
  365. }
  366. int spi_claim_bus(struct spi_slave *slave)
  367. {
  368. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  369. #ifdef CONFIG_SPI_HALF_DUPLEX
  370. ds->half_duplex = true;
  371. #else
  372. ds->half_duplex = false;
  373. #endif
  374. return __davinci_spi_claim_bus(ds, ds->slave.cs);
  375. }
  376. void spi_release_bus(struct spi_slave *slave)
  377. {
  378. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  379. __davinci_spi_release_bus(ds);
  380. }
  381. #else
  382. static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
  383. {
  384. struct davinci_spi_slave *ds = dev_get_priv(bus);
  385. debug("%s speed %u\n", __func__, max_hz);
  386. if (max_hz > CONFIG_SYS_SPI_CLK / 2)
  387. return -EINVAL;
  388. ds->freq = max_hz;
  389. return 0;
  390. }
  391. static int davinci_spi_set_mode(struct udevice *bus, uint mode)
  392. {
  393. struct davinci_spi_slave *ds = dev_get_priv(bus);
  394. debug("%s mode %u\n", __func__, mode);
  395. ds->mode = mode;
  396. return 0;
  397. }
  398. static int davinci_spi_claim_bus(struct udevice *dev)
  399. {
  400. struct dm_spi_slave_platdata *slave_plat =
  401. dev_get_parent_platdata(dev);
  402. struct udevice *bus = dev->parent;
  403. struct davinci_spi_slave *ds = dev_get_priv(bus);
  404. if (slave_plat->cs >= ds->num_cs) {
  405. printf("Invalid SPI chipselect\n");
  406. return -EINVAL;
  407. }
  408. ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
  409. return __davinci_spi_claim_bus(ds, slave_plat->cs);
  410. }
  411. static int davinci_spi_release_bus(struct udevice *dev)
  412. {
  413. struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
  414. return __davinci_spi_release_bus(ds);
  415. }
  416. static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
  417. const void *dout, void *din,
  418. unsigned long flags)
  419. {
  420. struct dm_spi_slave_platdata *slave =
  421. dev_get_parent_platdata(dev);
  422. struct udevice *bus = dev->parent;
  423. struct davinci_spi_slave *ds = dev_get_priv(bus);
  424. if (slave->cs >= ds->num_cs) {
  425. printf("Invalid SPI chipselect\n");
  426. return -EINVAL;
  427. }
  428. ds->cur_cs = slave->cs;
  429. return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
  430. }
  431. static int davinci_spi_probe(struct udevice *bus)
  432. {
  433. /* Nothing to do */
  434. return 0;
  435. }
  436. static int davinci_ofdata_to_platadata(struct udevice *bus)
  437. {
  438. struct davinci_spi_slave *ds = dev_get_priv(bus);
  439. const void *blob = gd->fdt_blob;
  440. int node = dev_of_offset(bus);
  441. ds->regs = devfdt_map_physmem(bus, sizeof(struct davinci_spi_regs));
  442. if (!ds->regs) {
  443. printf("%s: could not map device address\n", __func__);
  444. return -EINVAL;
  445. }
  446. ds->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
  447. return 0;
  448. }
  449. static const struct dm_spi_ops davinci_spi_ops = {
  450. .claim_bus = davinci_spi_claim_bus,
  451. .release_bus = davinci_spi_release_bus,
  452. .xfer = davinci_spi_xfer,
  453. .set_speed = davinci_spi_set_speed,
  454. .set_mode = davinci_spi_set_mode,
  455. };
  456. static const struct udevice_id davinci_spi_ids[] = {
  457. { .compatible = "ti,keystone-spi" },
  458. { .compatible = "ti,dm6441-spi" },
  459. { .compatible = "ti,da830-spi" },
  460. { }
  461. };
  462. U_BOOT_DRIVER(davinci_spi) = {
  463. .name = "davinci_spi",
  464. .id = UCLASS_SPI,
  465. .of_match = davinci_spi_ids,
  466. .ops = &davinci_spi_ops,
  467. .ofdata_to_platdata = davinci_ofdata_to_platadata,
  468. .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),
  469. .probe = davinci_spi_probe,
  470. };
  471. #endif