davinci_spi.c 13 KB

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