spi-orion.c 19 KB

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