spi-orion.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Marvell Orion SPI controller driver
  4. *
  5. * Author: Shadi Ammouri <[email protected]>
  6. * Copyright (C) 2007-2008 Marvell Ltd.
  7. */
  8. #include <linux/interrupt.h>
  9. #include <linux/delay.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/sizes.h>
  21. #include <asm/unaligned.h>
  22. #define DRIVER_NAME "orion_spi"
  23. /* Runtime PM autosuspend timeout: PM is fairly light on this driver */
  24. #define SPI_AUTOSUSPEND_TIMEOUT 200
  25. /* Some SoCs using this driver support up to 8 chip selects.
  26. * It is up to the implementer to only use the chip selects
  27. * that are available.
  28. */
  29. #define ORION_NUM_CHIPSELECTS 8
  30. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  31. #define ORION_SPI_IF_CTRL_REG 0x00
  32. #define ORION_SPI_IF_CONFIG_REG 0x04
  33. #define ORION_SPI_IF_RXLSBF BIT(14)
  34. #define ORION_SPI_IF_TXLSBF BIT(13)
  35. #define ORION_SPI_DATA_OUT_REG 0x08
  36. #define ORION_SPI_DATA_IN_REG 0x0c
  37. #define ORION_SPI_INT_CAUSE_REG 0x10
  38. #define ORION_SPI_TIMING_PARAMS_REG 0x18
  39. /* Register for the "Direct Mode" */
  40. #define SPI_DIRECT_WRITE_CONFIG_REG 0x20
  41. #define ORION_SPI_TMISO_SAMPLE_MASK (0x3 << 6)
  42. #define ORION_SPI_TMISO_SAMPLE_1 (1 << 6)
  43. #define ORION_SPI_TMISO_SAMPLE_2 (2 << 6)
  44. #define ORION_SPI_MODE_CPOL (1 << 11)
  45. #define ORION_SPI_MODE_CPHA (1 << 12)
  46. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  47. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  48. #define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
  49. #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
  50. ORION_SPI_MODE_CPHA)
  51. #define ORION_SPI_CS_MASK 0x1C
  52. #define ORION_SPI_CS_SHIFT 2
  53. #define ORION_SPI_CS(cs) ((cs << ORION_SPI_CS_SHIFT) & \
  54. ORION_SPI_CS_MASK)
  55. enum orion_spi_type {
  56. ORION_SPI,
  57. ARMADA_SPI,
  58. };
  59. struct orion_spi_dev {
  60. enum orion_spi_type typ;
  61. /*
  62. * min_divisor and max_hz should be exclusive, the only we can
  63. * have both is for managing the armada-370-spi case with old
  64. * device tree
  65. */
  66. unsigned long max_hz;
  67. unsigned int min_divisor;
  68. unsigned int max_divisor;
  69. u32 prescale_mask;
  70. bool is_errata_50mhz_ac;
  71. };
  72. struct orion_direct_acc {
  73. void __iomem *vaddr;
  74. u32 size;
  75. };
  76. struct orion_child_options {
  77. struct orion_direct_acc direct_access;
  78. };
  79. struct orion_spi {
  80. struct spi_master *master;
  81. void __iomem *base;
  82. struct clk *clk;
  83. struct clk *axi_clk;
  84. const struct orion_spi_dev *devdata;
  85. struct device *dev;
  86. struct orion_child_options child[ORION_NUM_CHIPSELECTS];
  87. };
  88. #ifdef CONFIG_PM
  89. static int orion_spi_runtime_suspend(struct device *dev);
  90. static int orion_spi_runtime_resume(struct device *dev);
  91. #endif
  92. static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
  93. {
  94. return orion_spi->base + reg;
  95. }
  96. static inline void
  97. orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  98. {
  99. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  100. u32 val;
  101. val = readl(reg_addr);
  102. val |= mask;
  103. writel(val, reg_addr);
  104. }
  105. static inline void
  106. orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
  107. {
  108. void __iomem *reg_addr = spi_reg(orion_spi, reg);
  109. u32 val;
  110. val = readl(reg_addr);
  111. val &= ~mask;
  112. writel(val, reg_addr);
  113. }
  114. static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  115. {
  116. u32 tclk_hz;
  117. u32 rate;
  118. u32 prescale;
  119. u32 reg;
  120. struct orion_spi *orion_spi;
  121. const struct orion_spi_dev *devdata;
  122. orion_spi = spi_master_get_devdata(spi->master);
  123. devdata = orion_spi->devdata;
  124. tclk_hz = clk_get_rate(orion_spi->clk);
  125. if (devdata->typ == ARMADA_SPI) {
  126. /*
  127. * Given the core_clk (tclk_hz) and the target rate (speed) we
  128. * determine the best values for SPR (in [0 .. 15]) and SPPR (in
  129. * [0..7]) such that
  130. *
  131. * core_clk / (SPR * 2 ** SPPR)
  132. *
  133. * is as big as possible but not bigger than speed.
  134. */
  135. /* best integer divider: */
  136. unsigned divider = DIV_ROUND_UP(tclk_hz, speed);
  137. unsigned spr, sppr;
  138. if (divider < 16) {
  139. /* This is the easy case, divider is less than 16 */
  140. spr = divider;
  141. sppr = 0;
  142. } else {
  143. unsigned two_pow_sppr;
  144. /*
  145. * Find the highest bit set in divider. This and the
  146. * three next bits define SPR (apart from rounding).
  147. * SPPR is then the number of zero bits that must be
  148. * appended:
  149. */
  150. sppr = fls(divider) - 4;
  151. /*
  152. * As SPR only has 4 bits, we have to round divider up
  153. * to the next multiple of 2 ** sppr.
  154. */
  155. two_pow_sppr = 1 << sppr;
  156. divider = (divider + two_pow_sppr - 1) & -two_pow_sppr;
  157. /*
  158. * recalculate sppr as rounding up divider might have
  159. * increased it enough to change the position of the
  160. * highest set bit. In this case the bit that now
  161. * doesn't make it into SPR is 0, so there is no need to
  162. * round again.
  163. */
  164. sppr = fls(divider) - 4;
  165. spr = divider >> sppr;
  166. /*
  167. * Now do range checking. SPR is constructed to have a
  168. * width of 4 bits, so this is fine for sure. So we
  169. * still need to check for sppr to fit into 3 bits:
  170. */
  171. if (sppr > 7)
  172. return -EINVAL;
  173. }
  174. prescale = ((sppr & 0x6) << 5) | ((sppr & 0x1) << 4) | spr;
  175. } else {
  176. /*
  177. * the supported rates are: 4,6,8...30
  178. * round up as we look for equal or less speed
  179. */
  180. rate = DIV_ROUND_UP(tclk_hz, speed);
  181. rate = roundup(rate, 2);
  182. /* check if requested speed is too small */
  183. if (rate > 30)
  184. return -EINVAL;
  185. if (rate < 4)
  186. rate = 4;
  187. /* Convert the rate to SPI clock divisor value. */
  188. prescale = 0x10 + rate/2;
  189. }
  190. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  191. reg = ((reg & ~devdata->prescale_mask) | prescale);
  192. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  193. return 0;
  194. }
  195. static void
  196. orion_spi_mode_set(struct spi_device *spi)
  197. {
  198. u32 reg;
  199. struct orion_spi *orion_spi;
  200. orion_spi = spi_master_get_devdata(spi->master);
  201. reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  202. reg &= ~ORION_SPI_MODE_MASK;
  203. if (spi->mode & SPI_CPOL)
  204. reg |= ORION_SPI_MODE_CPOL;
  205. if (spi->mode & SPI_CPHA)
  206. reg |= ORION_SPI_MODE_CPHA;
  207. if (spi->mode & SPI_LSB_FIRST)
  208. reg |= ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF;
  209. else
  210. reg &= ~(ORION_SPI_IF_RXLSBF | ORION_SPI_IF_TXLSBF);
  211. writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
  212. }
  213. static void
  214. orion_spi_50mhz_ac_timing_erratum(struct spi_device *spi, unsigned int speed)
  215. {
  216. u32 reg;
  217. struct orion_spi *orion_spi;
  218. orion_spi = spi_master_get_devdata(spi->master);
  219. /*
  220. * Erratum description: (Erratum NO. FE-9144572) The device
  221. * SPI interface supports frequencies of up to 50 MHz.
  222. * However, due to this erratum, when the device core clock is
  223. * 250 MHz and the SPI interfaces is configured for 50MHz SPI
  224. * clock and CPOL=CPHA=1 there might occur data corruption on
  225. * reads from the SPI device.
  226. * Erratum Workaround:
  227. * Work in one of the following configurations:
  228. * 1. Set CPOL=CPHA=0 in "SPI Interface Configuration
  229. * Register".
  230. * 2. Set TMISO_SAMPLE value to 0x2 in "SPI Timing Parameters 1
  231. * Register" before setting the interface.
  232. */
  233. reg = readl(spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
  234. reg &= ~ORION_SPI_TMISO_SAMPLE_MASK;
  235. if (clk_get_rate(orion_spi->clk) == 250000000 &&
  236. speed == 50000000 && spi->mode & SPI_CPOL &&
  237. spi->mode & SPI_CPHA)
  238. reg |= ORION_SPI_TMISO_SAMPLE_2;
  239. else
  240. reg |= ORION_SPI_TMISO_SAMPLE_1; /* This is the default value */
  241. writel(reg, spi_reg(orion_spi, ORION_SPI_TIMING_PARAMS_REG));
  242. }
  243. /*
  244. * called only when no transfer is active on the bus
  245. */
  246. static int
  247. orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  248. {
  249. struct orion_spi *orion_spi;
  250. unsigned int speed = spi->max_speed_hz;
  251. unsigned int bits_per_word = spi->bits_per_word;
  252. int rc;
  253. orion_spi = spi_master_get_devdata(spi->master);
  254. if ((t != NULL) && t->speed_hz)
  255. speed = t->speed_hz;
  256. if ((t != NULL) && t->bits_per_word)
  257. bits_per_word = t->bits_per_word;
  258. orion_spi_mode_set(spi);
  259. if (orion_spi->devdata->is_errata_50mhz_ac)
  260. orion_spi_50mhz_ac_timing_erratum(spi, speed);
  261. rc = orion_spi_baudrate_set(spi, speed);
  262. if (rc)
  263. return rc;
  264. if (bits_per_word == 16)
  265. orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  266. ORION_SPI_IF_8_16_BIT_MODE);
  267. else
  268. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
  269. ORION_SPI_IF_8_16_BIT_MODE);
  270. return 0;
  271. }
  272. static void orion_spi_set_cs(struct spi_device *spi, bool enable)
  273. {
  274. struct orion_spi *orion_spi;
  275. void __iomem *ctrl_reg;
  276. u32 val;
  277. orion_spi = spi_master_get_devdata(spi->master);
  278. ctrl_reg = spi_reg(orion_spi, ORION_SPI_IF_CTRL_REG);
  279. val = readl(ctrl_reg);
  280. /* Clear existing chip-select and assertion state */
  281. val &= ~(ORION_SPI_CS_MASK | 0x1);
  282. /*
  283. * If this line is using a GPIO to control chip select, this internal
  284. * .set_cs() function will still be called, so we clear any previous
  285. * chip select. The CS we activate will not have any elecrical effect,
  286. * as it is handled by a GPIO, but that doesn't matter. What we need
  287. * is to deassert the old chip select and assert some other chip select.
  288. */
  289. val |= ORION_SPI_CS(spi->chip_select);
  290. /*
  291. * Chip select logic is inverted from spi_set_cs(). For lines using a
  292. * GPIO to do chip select SPI_CS_HIGH is enforced and inversion happens
  293. * in the GPIO library, but we don't care about that, because in those
  294. * cases we are dealing with an unused native CS anyways so the polarity
  295. * doesn't matter.
  296. */
  297. if (!enable)
  298. val |= 0x1;
  299. /*
  300. * To avoid toggling unwanted chip selects update the register
  301. * with a single write.
  302. */
  303. writel(val, ctrl_reg);
  304. }
  305. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  306. {
  307. int i;
  308. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  309. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  310. return 1;
  311. udelay(1);
  312. }
  313. return -1;
  314. }
  315. static inline int
  316. orion_spi_write_read_8bit(struct spi_device *spi,
  317. const u8 **tx_buf, u8 **rx_buf)
  318. {
  319. void __iomem *tx_reg, *rx_reg, *int_reg;
  320. struct orion_spi *orion_spi;
  321. bool cs_single_byte;
  322. cs_single_byte = spi->mode & SPI_CS_WORD;
  323. orion_spi = spi_master_get_devdata(spi->master);
  324. if (cs_single_byte)
  325. orion_spi_set_cs(spi, 0);
  326. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  327. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  328. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  329. /* clear the interrupt cause register */
  330. writel(0x0, int_reg);
  331. if (tx_buf && *tx_buf)
  332. writel(*(*tx_buf)++, tx_reg);
  333. else
  334. writel(0, tx_reg);
  335. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  336. if (cs_single_byte) {
  337. orion_spi_set_cs(spi, 1);
  338. /* Satisfy some SLIC devices requirements */
  339. udelay(4);
  340. }
  341. dev_err(&spi->dev, "TXS timed out\n");
  342. return -1;
  343. }
  344. if (rx_buf && *rx_buf)
  345. *(*rx_buf)++ = readl(rx_reg);
  346. if (cs_single_byte) {
  347. orion_spi_set_cs(spi, 1);
  348. /* Satisfy some SLIC devices requirements */
  349. udelay(4);
  350. }
  351. return 1;
  352. }
  353. static inline int
  354. orion_spi_write_read_16bit(struct spi_device *spi,
  355. const u16 **tx_buf, u16 **rx_buf)
  356. {
  357. void __iomem *tx_reg, *rx_reg, *int_reg;
  358. struct orion_spi *orion_spi;
  359. if (spi->mode & SPI_CS_WORD) {
  360. dev_err(&spi->dev, "SPI_CS_WORD is only supported for 8 bit words\n");
  361. return -1;
  362. }
  363. orion_spi = spi_master_get_devdata(spi->master);
  364. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  365. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  366. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  367. /* clear the interrupt cause register */
  368. writel(0x0, int_reg);
  369. if (tx_buf && *tx_buf)
  370. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  371. else
  372. writel(0, tx_reg);
  373. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  374. dev_err(&spi->dev, "TXS timed out\n");
  375. return -1;
  376. }
  377. if (rx_buf && *rx_buf)
  378. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  379. return 1;
  380. }
  381. static unsigned int
  382. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  383. {
  384. unsigned int count;
  385. int word_len;
  386. struct orion_spi *orion_spi;
  387. int cs = spi->chip_select;
  388. void __iomem *vaddr;
  389. word_len = spi->bits_per_word;
  390. count = xfer->len;
  391. orion_spi = spi_master_get_devdata(spi->master);
  392. /*
  393. * Use SPI direct write mode if base address is available
  394. * and SPI_CS_WORD flag is not set.
  395. * Otherwise fall back to PIO mode for this transfer.
  396. */
  397. vaddr = orion_spi->child[cs].direct_access.vaddr;
  398. if (vaddr && xfer->tx_buf && word_len == 8 && (spi->mode & SPI_CS_WORD) == 0) {
  399. unsigned int cnt = count / 4;
  400. unsigned int rem = count % 4;
  401. /*
  402. * Send the TX-data to the SPI device via the direct
  403. * mapped address window
  404. */
  405. iowrite32_rep(vaddr, xfer->tx_buf, cnt);
  406. if (rem) {
  407. u32 *buf = (u32 *)xfer->tx_buf;
  408. iowrite8_rep(vaddr, &buf[cnt], rem);
  409. }
  410. return count;
  411. }
  412. if (word_len == 8) {
  413. const u8 *tx = xfer->tx_buf;
  414. u8 *rx = xfer->rx_buf;
  415. do {
  416. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  417. goto out;
  418. count--;
  419. spi_delay_exec(&xfer->word_delay, xfer);
  420. } while (count);
  421. } else if (word_len == 16) {
  422. const u16 *tx = xfer->tx_buf;
  423. u16 *rx = xfer->rx_buf;
  424. do {
  425. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  426. goto out;
  427. count -= 2;
  428. spi_delay_exec(&xfer->word_delay, xfer);
  429. } while (count);
  430. }
  431. out:
  432. return xfer->len - count;
  433. }
  434. static int orion_spi_transfer_one(struct spi_master *master,
  435. struct spi_device *spi,
  436. struct spi_transfer *t)
  437. {
  438. int status = 0;
  439. status = orion_spi_setup_transfer(spi, t);
  440. if (status < 0)
  441. return status;
  442. if (t->len)
  443. orion_spi_write_read(spi, t);
  444. return status;
  445. }
  446. static int orion_spi_setup(struct spi_device *spi)
  447. {
  448. int ret;
  449. #ifdef CONFIG_PM
  450. struct orion_spi *orion_spi = spi_master_get_devdata(spi->master);
  451. struct device *dev = orion_spi->dev;
  452. orion_spi_runtime_resume(dev);
  453. #endif
  454. ret = orion_spi_setup_transfer(spi, NULL);
  455. #ifdef CONFIG_PM
  456. orion_spi_runtime_suspend(dev);
  457. #endif
  458. return ret;
  459. }
  460. static int orion_spi_reset(struct orion_spi *orion_spi)
  461. {
  462. /* Verify that the CS is deasserted */
  463. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  464. /* Don't deassert CS between the direct mapped SPI transfers */
  465. writel(0, spi_reg(orion_spi, SPI_DIRECT_WRITE_CONFIG_REG));
  466. return 0;
  467. }
  468. static const struct orion_spi_dev orion_spi_dev_data = {
  469. .typ = ORION_SPI,
  470. .min_divisor = 4,
  471. .max_divisor = 30,
  472. .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
  473. };
  474. static const struct orion_spi_dev armada_370_spi_dev_data = {
  475. .typ = ARMADA_SPI,
  476. .min_divisor = 4,
  477. .max_divisor = 1920,
  478. .max_hz = 50000000,
  479. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  480. };
  481. static const struct orion_spi_dev armada_xp_spi_dev_data = {
  482. .typ = ARMADA_SPI,
  483. .max_hz = 50000000,
  484. .max_divisor = 1920,
  485. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  486. };
  487. static const struct orion_spi_dev armada_375_spi_dev_data = {
  488. .typ = ARMADA_SPI,
  489. .min_divisor = 15,
  490. .max_divisor = 1920,
  491. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  492. };
  493. static const struct orion_spi_dev armada_380_spi_dev_data = {
  494. .typ = ARMADA_SPI,
  495. .max_hz = 50000000,
  496. .max_divisor = 1920,
  497. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  498. .is_errata_50mhz_ac = true,
  499. };
  500. static const struct of_device_id orion_spi_of_match_table[] = {
  501. {
  502. .compatible = "marvell,orion-spi",
  503. .data = &orion_spi_dev_data,
  504. },
  505. {
  506. .compatible = "marvell,armada-370-spi",
  507. .data = &armada_370_spi_dev_data,
  508. },
  509. {
  510. .compatible = "marvell,armada-375-spi",
  511. .data = &armada_375_spi_dev_data,
  512. },
  513. {
  514. .compatible = "marvell,armada-380-spi",
  515. .data = &armada_380_spi_dev_data,
  516. },
  517. {
  518. .compatible = "marvell,armada-390-spi",
  519. .data = &armada_xp_spi_dev_data,
  520. },
  521. {
  522. .compatible = "marvell,armada-xp-spi",
  523. .data = &armada_xp_spi_dev_data,
  524. },
  525. {}
  526. };
  527. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  528. static int orion_spi_probe(struct platform_device *pdev)
  529. {
  530. const struct orion_spi_dev *devdata;
  531. struct spi_master *master;
  532. struct orion_spi *spi;
  533. struct resource *r;
  534. unsigned long tclk_hz;
  535. int status = 0;
  536. struct device_node *np;
  537. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  538. if (master == NULL) {
  539. dev_dbg(&pdev->dev, "master allocation failed\n");
  540. return -ENOMEM;
  541. }
  542. if (pdev->id != -1)
  543. master->bus_num = pdev->id;
  544. if (pdev->dev.of_node) {
  545. u32 cell_index;
  546. if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
  547. &cell_index))
  548. master->bus_num = cell_index;
  549. }
  550. /* we support all 4 SPI modes and LSB first option */
  551. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST | SPI_CS_WORD;
  552. master->set_cs = orion_spi_set_cs;
  553. master->transfer_one = orion_spi_transfer_one;
  554. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  555. master->setup = orion_spi_setup;
  556. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  557. master->auto_runtime_pm = true;
  558. master->use_gpio_descriptors = true;
  559. master->flags = SPI_MASTER_GPIO_SS;
  560. platform_set_drvdata(pdev, master);
  561. spi = spi_master_get_devdata(master);
  562. spi->master = master;
  563. spi->dev = &pdev->dev;
  564. devdata = device_get_match_data(&pdev->dev);
  565. devdata = devdata ? devdata : &orion_spi_dev_data;
  566. spi->devdata = devdata;
  567. spi->clk = devm_clk_get(&pdev->dev, NULL);
  568. if (IS_ERR(spi->clk)) {
  569. status = PTR_ERR(spi->clk);
  570. goto out;
  571. }
  572. status = clk_prepare_enable(spi->clk);
  573. if (status)
  574. goto out;
  575. /* The following clock is only used by some SoCs */
  576. spi->axi_clk = devm_clk_get(&pdev->dev, "axi");
  577. if (PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) {
  578. status = -EPROBE_DEFER;
  579. goto out_rel_clk;
  580. }
  581. if (!IS_ERR(spi->axi_clk))
  582. clk_prepare_enable(spi->axi_clk);
  583. tclk_hz = clk_get_rate(spi->clk);
  584. /*
  585. * With old device tree, armada-370-spi could be used with
  586. * Armada XP, however for this SoC the maximum frequency is
  587. * 50MHz instead of tclk/4. On Armada 370, tclk cannot be
  588. * higher than 200MHz. So, in order to be able to handle both
  589. * SoCs, we can take the minimum of 50MHz and tclk/4.
  590. */
  591. if (of_device_is_compatible(pdev->dev.of_node,
  592. "marvell,armada-370-spi"))
  593. master->max_speed_hz = min(devdata->max_hz,
  594. DIV_ROUND_UP(tclk_hz, devdata->min_divisor));
  595. else if (devdata->min_divisor)
  596. master->max_speed_hz =
  597. DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
  598. else
  599. master->max_speed_hz = devdata->max_hz;
  600. master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
  601. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  602. spi->base = devm_ioremap_resource(&pdev->dev, r);
  603. if (IS_ERR(spi->base)) {
  604. status = PTR_ERR(spi->base);
  605. goto out_rel_axi_clk;
  606. }
  607. for_each_available_child_of_node(pdev->dev.of_node, np) {
  608. struct orion_direct_acc *dir_acc;
  609. u32 cs;
  610. /* Get chip-select number from the "reg" property */
  611. status = of_property_read_u32(np, "reg", &cs);
  612. if (status) {
  613. dev_err(&pdev->dev,
  614. "%pOF has no valid 'reg' property (%d)\n",
  615. np, status);
  616. continue;
  617. }
  618. /*
  619. * Check if an address is configured for this SPI device. If
  620. * not, the MBus mapping via the 'ranges' property in the 'soc'
  621. * node is not configured and this device should not use the
  622. * direct mode. In this case, just continue with the next
  623. * device.
  624. */
  625. status = of_address_to_resource(pdev->dev.of_node, cs + 1, r);
  626. if (status)
  627. continue;
  628. /*
  629. * Only map one page for direct access. This is enough for the
  630. * simple TX transfer which only writes to the first word.
  631. * This needs to get extended for the direct SPI NOR / SPI NAND
  632. * support, once this gets implemented.
  633. */
  634. dir_acc = &spi->child[cs].direct_access;
  635. dir_acc->vaddr = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
  636. if (!dir_acc->vaddr) {
  637. status = -ENOMEM;
  638. of_node_put(np);
  639. goto out_rel_axi_clk;
  640. }
  641. dir_acc->size = PAGE_SIZE;
  642. dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
  643. }
  644. pm_runtime_set_active(&pdev->dev);
  645. pm_runtime_use_autosuspend(&pdev->dev);
  646. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  647. pm_runtime_enable(&pdev->dev);
  648. status = orion_spi_reset(spi);
  649. if (status < 0)
  650. goto out_rel_pm;
  651. master->dev.of_node = pdev->dev.of_node;
  652. status = spi_register_master(master);
  653. if (status < 0)
  654. goto out_rel_pm;
  655. return status;
  656. out_rel_pm:
  657. pm_runtime_disable(&pdev->dev);
  658. out_rel_axi_clk:
  659. clk_disable_unprepare(spi->axi_clk);
  660. out_rel_clk:
  661. clk_disable_unprepare(spi->clk);
  662. out:
  663. spi_master_put(master);
  664. return status;
  665. }
  666. static int orion_spi_remove(struct platform_device *pdev)
  667. {
  668. struct spi_master *master = platform_get_drvdata(pdev);
  669. struct orion_spi *spi = spi_master_get_devdata(master);
  670. pm_runtime_get_sync(&pdev->dev);
  671. clk_disable_unprepare(spi->axi_clk);
  672. clk_disable_unprepare(spi->clk);
  673. spi_unregister_master(master);
  674. pm_runtime_disable(&pdev->dev);
  675. return 0;
  676. }
  677. MODULE_ALIAS("platform:" DRIVER_NAME);
  678. #ifdef CONFIG_PM
  679. static int orion_spi_runtime_suspend(struct device *dev)
  680. {
  681. struct spi_master *master = dev_get_drvdata(dev);
  682. struct orion_spi *spi = spi_master_get_devdata(master);
  683. clk_disable_unprepare(spi->axi_clk);
  684. clk_disable_unprepare(spi->clk);
  685. return 0;
  686. }
  687. static int orion_spi_runtime_resume(struct device *dev)
  688. {
  689. struct spi_master *master = dev_get_drvdata(dev);
  690. struct orion_spi *spi = spi_master_get_devdata(master);
  691. if (!IS_ERR(spi->axi_clk))
  692. clk_prepare_enable(spi->axi_clk);
  693. return clk_prepare_enable(spi->clk);
  694. }
  695. #endif
  696. static const struct dev_pm_ops orion_spi_pm_ops = {
  697. SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
  698. orion_spi_runtime_resume,
  699. NULL)
  700. };
  701. static struct platform_driver orion_spi_driver = {
  702. .driver = {
  703. .name = DRIVER_NAME,
  704. .pm = &orion_spi_pm_ops,
  705. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  706. },
  707. .probe = orion_spi_probe,
  708. .remove = orion_spi_remove,
  709. };
  710. module_platform_driver(orion_spi_driver);
  711. MODULE_DESCRIPTION("Orion SPI driver");
  712. MODULE_AUTHOR("Shadi Ammouri <[email protected]>");
  713. MODULE_LICENSE("GPL");