spi-orion.c 19 KB

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