spi-orion.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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 <linux/gpio.h>
  25. #include <asm/unaligned.h>
  26. #define DRIVER_NAME "orion_spi"
  27. /* Runtime PM autosuspend timeout: PM is fairly light on this driver */
  28. #define SPI_AUTOSUSPEND_TIMEOUT 200
  29. /* Some SoCs using this driver support up to 8 chip selects.
  30. * It is up to the implementer to only use the chip selects
  31. * that are available.
  32. */
  33. #define ORION_NUM_CHIPSELECTS 8
  34. #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
  35. #define ORION_SPI_IF_CTRL_REG 0x00
  36. #define ORION_SPI_IF_CONFIG_REG 0x04
  37. #define ORION_SPI_IF_RXLSBF BIT(14)
  38. #define ORION_SPI_IF_TXLSBF BIT(13)
  39. #define ORION_SPI_DATA_OUT_REG 0x08
  40. #define ORION_SPI_DATA_IN_REG 0x0c
  41. #define ORION_SPI_INT_CAUSE_REG 0x10
  42. #define ORION_SPI_TIMING_PARAMS_REG 0x18
  43. /* Register for the "Direct Mode" */
  44. #define SPI_DIRECT_WRITE_CONFIG_REG 0x20
  45. #define ORION_SPI_TMISO_SAMPLE_MASK (0x3 << 6)
  46. #define ORION_SPI_TMISO_SAMPLE_1 (1 << 6)
  47. #define ORION_SPI_TMISO_SAMPLE_2 (2 << 6)
  48. #define ORION_SPI_MODE_CPOL (1 << 11)
  49. #define ORION_SPI_MODE_CPHA (1 << 12)
  50. #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
  51. #define ORION_SPI_CLK_PRESCALE_MASK 0x1F
  52. #define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
  53. #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
  54. ORION_SPI_MODE_CPHA)
  55. #define ORION_SPI_CS_MASK 0x1C
  56. #define ORION_SPI_CS_SHIFT 2
  57. #define ORION_SPI_CS(cs) ((cs << ORION_SPI_CS_SHIFT) & \
  58. ORION_SPI_CS_MASK)
  59. enum orion_spi_type {
  60. ORION_SPI,
  61. ARMADA_SPI,
  62. };
  63. struct orion_spi_dev {
  64. enum orion_spi_type typ;
  65. /*
  66. * min_divisor and max_hz should be exclusive, the only we can
  67. * have both is for managing the armada-370-spi case with old
  68. * device tree
  69. */
  70. unsigned long max_hz;
  71. unsigned int min_divisor;
  72. unsigned int max_divisor;
  73. u32 prescale_mask;
  74. bool is_errata_50mhz_ac;
  75. };
  76. struct orion_direct_acc {
  77. void __iomem *vaddr;
  78. u32 size;
  79. };
  80. struct orion_child_options {
  81. struct orion_direct_acc direct_access;
  82. };
  83. struct orion_spi {
  84. struct spi_master *master;
  85. void __iomem *base;
  86. struct clk *clk;
  87. struct clk *axi_clk;
  88. const struct orion_spi_dev *devdata;
  89. int unused_hw_gpio;
  90. struct orion_child_options child[ORION_NUM_CHIPSELECTS];
  91. };
  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. int cs;
  276. orion_spi = spi_master_get_devdata(spi->master);
  277. if (gpio_is_valid(spi->cs_gpio))
  278. cs = orion_spi->unused_hw_gpio;
  279. else
  280. cs = spi->chip_select;
  281. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, ORION_SPI_CS_MASK);
  282. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG,
  283. ORION_SPI_CS(cs));
  284. /* Chip select logic is inverted from spi_set_cs */
  285. if (!enable)
  286. orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  287. else
  288. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  289. }
  290. static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
  291. {
  292. int i;
  293. for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
  294. if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
  295. return 1;
  296. udelay(1);
  297. }
  298. return -1;
  299. }
  300. static inline int
  301. orion_spi_write_read_8bit(struct spi_device *spi,
  302. const u8 **tx_buf, u8 **rx_buf)
  303. {
  304. void __iomem *tx_reg, *rx_reg, *int_reg;
  305. struct orion_spi *orion_spi;
  306. orion_spi = spi_master_get_devdata(spi->master);
  307. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  308. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  309. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  310. /* clear the interrupt cause register */
  311. writel(0x0, int_reg);
  312. if (tx_buf && *tx_buf)
  313. writel(*(*tx_buf)++, tx_reg);
  314. else
  315. writel(0, tx_reg);
  316. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  317. dev_err(&spi->dev, "TXS timed out\n");
  318. return -1;
  319. }
  320. if (rx_buf && *rx_buf)
  321. *(*rx_buf)++ = readl(rx_reg);
  322. return 1;
  323. }
  324. static inline int
  325. orion_spi_write_read_16bit(struct spi_device *spi,
  326. const u16 **tx_buf, u16 **rx_buf)
  327. {
  328. void __iomem *tx_reg, *rx_reg, *int_reg;
  329. struct orion_spi *orion_spi;
  330. orion_spi = spi_master_get_devdata(spi->master);
  331. tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
  332. rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
  333. int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
  334. /* clear the interrupt cause register */
  335. writel(0x0, int_reg);
  336. if (tx_buf && *tx_buf)
  337. writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
  338. else
  339. writel(0, tx_reg);
  340. if (orion_spi_wait_till_ready(orion_spi) < 0) {
  341. dev_err(&spi->dev, "TXS timed out\n");
  342. return -1;
  343. }
  344. if (rx_buf && *rx_buf)
  345. put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
  346. return 1;
  347. }
  348. static unsigned int
  349. orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  350. {
  351. unsigned int count;
  352. int word_len;
  353. struct orion_spi *orion_spi;
  354. int cs = spi->chip_select;
  355. word_len = spi->bits_per_word;
  356. count = xfer->len;
  357. orion_spi = spi_master_get_devdata(spi->master);
  358. /*
  359. * Use SPI direct write mode if base address is available. Otherwise
  360. * fall back to PIO mode for this transfer.
  361. */
  362. if ((orion_spi->child[cs].direct_access.vaddr) && (xfer->tx_buf) &&
  363. (word_len == 8)) {
  364. unsigned int cnt = count / 4;
  365. unsigned int rem = count % 4;
  366. /*
  367. * Send the TX-data to the SPI device via the direct
  368. * mapped address window
  369. */
  370. iowrite32_rep(orion_spi->child[cs].direct_access.vaddr,
  371. xfer->tx_buf, cnt);
  372. if (rem) {
  373. u32 *buf = (u32 *)xfer->tx_buf;
  374. iowrite8_rep(orion_spi->child[cs].direct_access.vaddr,
  375. &buf[cnt], rem);
  376. }
  377. return count;
  378. }
  379. if (word_len == 8) {
  380. const u8 *tx = xfer->tx_buf;
  381. u8 *rx = xfer->rx_buf;
  382. do {
  383. if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
  384. goto out;
  385. count--;
  386. } while (count);
  387. } else if (word_len == 16) {
  388. const u16 *tx = xfer->tx_buf;
  389. u16 *rx = xfer->rx_buf;
  390. do {
  391. if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
  392. goto out;
  393. count -= 2;
  394. } while (count);
  395. }
  396. out:
  397. return xfer->len - count;
  398. }
  399. static int orion_spi_transfer_one(struct spi_master *master,
  400. struct spi_device *spi,
  401. struct spi_transfer *t)
  402. {
  403. int status = 0;
  404. status = orion_spi_setup_transfer(spi, t);
  405. if (status < 0)
  406. return status;
  407. if (t->len)
  408. orion_spi_write_read(spi, t);
  409. return status;
  410. }
  411. static int orion_spi_setup(struct spi_device *spi)
  412. {
  413. if (gpio_is_valid(spi->cs_gpio)) {
  414. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  415. }
  416. return orion_spi_setup_transfer(spi, NULL);
  417. }
  418. static int orion_spi_reset(struct orion_spi *orion_spi)
  419. {
  420. /* Verify that the CS is deasserted */
  421. orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
  422. /* Don't deassert CS between the direct mapped SPI transfers */
  423. writel(0, spi_reg(orion_spi, SPI_DIRECT_WRITE_CONFIG_REG));
  424. return 0;
  425. }
  426. static const struct orion_spi_dev orion_spi_dev_data = {
  427. .typ = ORION_SPI,
  428. .min_divisor = 4,
  429. .max_divisor = 30,
  430. .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
  431. };
  432. static const struct orion_spi_dev armada_370_spi_dev_data = {
  433. .typ = ARMADA_SPI,
  434. .min_divisor = 4,
  435. .max_divisor = 1920,
  436. .max_hz = 50000000,
  437. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  438. };
  439. static const struct orion_spi_dev armada_xp_spi_dev_data = {
  440. .typ = ARMADA_SPI,
  441. .max_hz = 50000000,
  442. .max_divisor = 1920,
  443. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  444. };
  445. static const struct orion_spi_dev armada_375_spi_dev_data = {
  446. .typ = ARMADA_SPI,
  447. .min_divisor = 15,
  448. .max_divisor = 1920,
  449. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  450. };
  451. static const struct orion_spi_dev armada_380_spi_dev_data = {
  452. .typ = ARMADA_SPI,
  453. .max_hz = 50000000,
  454. .max_divisor = 1920,
  455. .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
  456. .is_errata_50mhz_ac = true,
  457. };
  458. static const struct of_device_id orion_spi_of_match_table[] = {
  459. {
  460. .compatible = "marvell,orion-spi",
  461. .data = &orion_spi_dev_data,
  462. },
  463. {
  464. .compatible = "marvell,armada-370-spi",
  465. .data = &armada_370_spi_dev_data,
  466. },
  467. {
  468. .compatible = "marvell,armada-375-spi",
  469. .data = &armada_375_spi_dev_data,
  470. },
  471. {
  472. .compatible = "marvell,armada-380-spi",
  473. .data = &armada_380_spi_dev_data,
  474. },
  475. {
  476. .compatible = "marvell,armada-390-spi",
  477. .data = &armada_xp_spi_dev_data,
  478. },
  479. {
  480. .compatible = "marvell,armada-xp-spi",
  481. .data = &armada_xp_spi_dev_data,
  482. },
  483. {}
  484. };
  485. MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
  486. static int orion_spi_probe(struct platform_device *pdev)
  487. {
  488. const struct of_device_id *of_id;
  489. const struct orion_spi_dev *devdata;
  490. struct spi_master *master;
  491. struct orion_spi *spi;
  492. struct resource *r;
  493. unsigned long tclk_hz;
  494. int status = 0;
  495. struct device_node *np;
  496. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  497. if (master == NULL) {
  498. dev_dbg(&pdev->dev, "master allocation failed\n");
  499. return -ENOMEM;
  500. }
  501. if (pdev->id != -1)
  502. master->bus_num = pdev->id;
  503. if (pdev->dev.of_node) {
  504. u32 cell_index;
  505. if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
  506. &cell_index))
  507. master->bus_num = cell_index;
  508. }
  509. /* we support all 4 SPI modes and LSB first option */
  510. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
  511. master->set_cs = orion_spi_set_cs;
  512. master->transfer_one = orion_spi_transfer_one;
  513. master->num_chipselect = ORION_NUM_CHIPSELECTS;
  514. master->setup = orion_spi_setup;
  515. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  516. master->auto_runtime_pm = true;
  517. master->flags = SPI_MASTER_GPIO_SS;
  518. platform_set_drvdata(pdev, master);
  519. spi = spi_master_get_devdata(master);
  520. spi->master = master;
  521. spi->unused_hw_gpio = -1;
  522. of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
  523. devdata = (of_id) ? of_id->data : &orion_spi_dev_data;
  524. spi->devdata = devdata;
  525. spi->clk = devm_clk_get(&pdev->dev, NULL);
  526. if (IS_ERR(spi->clk)) {
  527. status = PTR_ERR(spi->clk);
  528. goto out;
  529. }
  530. status = clk_prepare_enable(spi->clk);
  531. if (status)
  532. goto out;
  533. /* The following clock is only used by some SoCs */
  534. spi->axi_clk = devm_clk_get(&pdev->dev, "axi");
  535. if (IS_ERR(spi->axi_clk) &&
  536. PTR_ERR(spi->axi_clk) == -EPROBE_DEFER) {
  537. status = -EPROBE_DEFER;
  538. goto out_rel_clk;
  539. }
  540. if (!IS_ERR(spi->axi_clk))
  541. clk_prepare_enable(spi->axi_clk);
  542. tclk_hz = clk_get_rate(spi->clk);
  543. /*
  544. * With old device tree, armada-370-spi could be used with
  545. * Armada XP, however for this SoC the maximum frequency is
  546. * 50MHz instead of tclk/4. On Armada 370, tclk cannot be
  547. * higher than 200MHz. So, in order to be able to handle both
  548. * SoCs, we can take the minimum of 50MHz and tclk/4.
  549. */
  550. if (of_device_is_compatible(pdev->dev.of_node,
  551. "marvell,armada-370-spi"))
  552. master->max_speed_hz = min(devdata->max_hz,
  553. DIV_ROUND_UP(tclk_hz, devdata->min_divisor));
  554. else if (devdata->min_divisor)
  555. master->max_speed_hz =
  556. DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
  557. else
  558. master->max_speed_hz = devdata->max_hz;
  559. master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
  560. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  561. spi->base = devm_ioremap_resource(&pdev->dev, r);
  562. if (IS_ERR(spi->base)) {
  563. status = PTR_ERR(spi->base);
  564. goto out_rel_axi_clk;
  565. }
  566. /* Scan all SPI devices of this controller for direct mapped devices */
  567. for_each_available_child_of_node(pdev->dev.of_node, np) {
  568. u32 cs;
  569. /* Get chip-select number from the "reg" property */
  570. status = of_property_read_u32(np, "reg", &cs);
  571. if (status) {
  572. dev_err(&pdev->dev,
  573. "%pOF has no valid 'reg' property (%d)\n",
  574. np, status);
  575. continue;
  576. }
  577. /*
  578. * Check if an address is configured for this SPI device. If
  579. * not, the MBus mapping via the 'ranges' property in the 'soc'
  580. * node is not configured and this device should not use the
  581. * direct mode. In this case, just continue with the next
  582. * device.
  583. */
  584. status = of_address_to_resource(pdev->dev.of_node, cs + 1, r);
  585. if (status)
  586. continue;
  587. /*
  588. * Only map one page for direct access. This is enough for the
  589. * simple TX transfer which only writes to the first word.
  590. * This needs to get extended for the direct SPI-NOR / SPI-NAND
  591. * support, once this gets implemented.
  592. */
  593. spi->child[cs].direct_access.vaddr = devm_ioremap(&pdev->dev,
  594. r->start,
  595. PAGE_SIZE);
  596. if (!spi->child[cs].direct_access.vaddr) {
  597. status = -ENOMEM;
  598. goto out_rel_axi_clk;
  599. }
  600. spi->child[cs].direct_access.size = PAGE_SIZE;
  601. dev_info(&pdev->dev, "CS%d configured for direct access\n", cs);
  602. }
  603. pm_runtime_set_active(&pdev->dev);
  604. pm_runtime_use_autosuspend(&pdev->dev);
  605. pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
  606. pm_runtime_enable(&pdev->dev);
  607. status = orion_spi_reset(spi);
  608. if (status < 0)
  609. goto out_rel_pm;
  610. pm_runtime_mark_last_busy(&pdev->dev);
  611. pm_runtime_put_autosuspend(&pdev->dev);
  612. master->dev.of_node = pdev->dev.of_node;
  613. status = spi_register_master(master);
  614. if (status < 0)
  615. goto out_rel_pm;
  616. if (master->cs_gpios) {
  617. int i;
  618. for (i = 0; i < master->num_chipselect; ++i) {
  619. char *gpio_name;
  620. if (!gpio_is_valid(master->cs_gpios[i])) {
  621. continue;
  622. }
  623. gpio_name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  624. "%s-CS%d", dev_name(&pdev->dev), i);
  625. if (!gpio_name) {
  626. status = -ENOMEM;
  627. goto out_rel_master;
  628. }
  629. status = devm_gpio_request(&pdev->dev,
  630. master->cs_gpios[i], gpio_name);
  631. if (status) {
  632. dev_err(&pdev->dev,
  633. "Can't request GPIO for CS %d\n",
  634. master->cs_gpios[i]);
  635. goto out_rel_master;
  636. }
  637. if (spi->unused_hw_gpio == -1) {
  638. dev_info(&pdev->dev,
  639. "Selected unused HW CS#%d for any GPIO CSes\n",
  640. i);
  641. spi->unused_hw_gpio = i;
  642. }
  643. }
  644. }
  645. return status;
  646. out_rel_master:
  647. spi_unregister_master(master);
  648. out_rel_pm:
  649. pm_runtime_disable(&pdev->dev);
  650. out_rel_axi_clk:
  651. clk_disable_unprepare(spi->axi_clk);
  652. out_rel_clk:
  653. clk_disable_unprepare(spi->clk);
  654. out:
  655. spi_master_put(master);
  656. return status;
  657. }
  658. static int orion_spi_remove(struct platform_device *pdev)
  659. {
  660. struct spi_master *master = platform_get_drvdata(pdev);
  661. struct orion_spi *spi = spi_master_get_devdata(master);
  662. pm_runtime_get_sync(&pdev->dev);
  663. clk_disable_unprepare(spi->axi_clk);
  664. clk_disable_unprepare(spi->clk);
  665. spi_unregister_master(master);
  666. pm_runtime_disable(&pdev->dev);
  667. return 0;
  668. }
  669. MODULE_ALIAS("platform:" DRIVER_NAME);
  670. #ifdef CONFIG_PM
  671. static int orion_spi_runtime_suspend(struct device *dev)
  672. {
  673. struct spi_master *master = dev_get_drvdata(dev);
  674. struct orion_spi *spi = spi_master_get_devdata(master);
  675. clk_disable_unprepare(spi->axi_clk);
  676. clk_disable_unprepare(spi->clk);
  677. return 0;
  678. }
  679. static int orion_spi_runtime_resume(struct device *dev)
  680. {
  681. struct spi_master *master = dev_get_drvdata(dev);
  682. struct orion_spi *spi = spi_master_get_devdata(master);
  683. if (!IS_ERR(spi->axi_clk))
  684. clk_prepare_enable(spi->axi_clk);
  685. return clk_prepare_enable(spi->clk);
  686. }
  687. #endif
  688. static const struct dev_pm_ops orion_spi_pm_ops = {
  689. SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
  690. orion_spi_runtime_resume,
  691. NULL)
  692. };
  693. static struct platform_driver orion_spi_driver = {
  694. .driver = {
  695. .name = DRIVER_NAME,
  696. .pm = &orion_spi_pm_ops,
  697. .of_match_table = of_match_ptr(orion_spi_of_match_table),
  698. },
  699. .probe = orion_spi_probe,
  700. .remove = orion_spi_remove,
  701. };
  702. module_platform_driver(orion_spi_driver);
  703. MODULE_DESCRIPTION("Orion SPI driver");
  704. MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
  705. MODULE_LICENSE("GPL");