spi_mpc8xxx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. * MPC8xxx SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * CPM SPI and QE buffer descriptors mode support:
  9. * Copyright (c) 2009 MontaVista Software, Inc.
  10. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bug.h>
  22. #include <linux/errno.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/completion.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/device.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/fsl_devices.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/mm.h>
  36. #include <linux/mutex.h>
  37. #include <linux/of.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/gpio.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/slab.h>
  42. #include <sysdev/fsl_soc.h>
  43. #include <asm/cpm.h>
  44. #include <asm/qe.h>
  45. #include <asm/irq.h>
  46. /* CPM1 and CPM2 are mutually exclusive. */
  47. #ifdef CONFIG_CPM1
  48. #include <asm/cpm1.h>
  49. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  50. #else
  51. #include <asm/cpm2.h>
  52. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  53. #endif
  54. /* SPI Controller registers */
  55. struct mpc8xxx_spi_reg {
  56. u8 res1[0x20];
  57. __be32 mode;
  58. __be32 event;
  59. __be32 mask;
  60. __be32 command;
  61. __be32 transmit;
  62. __be32 receive;
  63. };
  64. /* SPI Controller mode register definitions */
  65. #define SPMODE_LOOP (1 << 30)
  66. #define SPMODE_CI_INACTIVEHIGH (1 << 29)
  67. #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
  68. #define SPMODE_DIV16 (1 << 27)
  69. #define SPMODE_REV (1 << 26)
  70. #define SPMODE_MS (1 << 25)
  71. #define SPMODE_ENABLE (1 << 24)
  72. #define SPMODE_LEN(x) ((x) << 20)
  73. #define SPMODE_PM(x) ((x) << 16)
  74. #define SPMODE_OP (1 << 14)
  75. #define SPMODE_CG(x) ((x) << 7)
  76. /*
  77. * Default for SPI Mode:
  78. * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
  79. */
  80. #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
  81. SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
  82. /* SPIE register values */
  83. #define SPIE_NE 0x00000200 /* Not empty */
  84. #define SPIE_NF 0x00000100 /* Not full */
  85. /* SPIM register values */
  86. #define SPIM_NE 0x00000200 /* Not empty */
  87. #define SPIM_NF 0x00000100 /* Not full */
  88. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  89. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  90. /* SPCOM register values */
  91. #define SPCOM_STR (1 << 23) /* Start transmit */
  92. #define SPI_PRAM_SIZE 0x100
  93. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  94. /* SPI Controller driver's private data. */
  95. struct mpc8xxx_spi {
  96. struct device *dev;
  97. struct mpc8xxx_spi_reg __iomem *base;
  98. /* rx & tx bufs from the spi_transfer */
  99. const void *tx;
  100. void *rx;
  101. int subblock;
  102. struct spi_pram __iomem *pram;
  103. struct cpm_buf_desc __iomem *tx_bd;
  104. struct cpm_buf_desc __iomem *rx_bd;
  105. struct spi_transfer *xfer_in_progress;
  106. /* dma addresses for CPM transfers */
  107. dma_addr_t tx_dma;
  108. dma_addr_t rx_dma;
  109. bool map_tx_dma;
  110. bool map_rx_dma;
  111. dma_addr_t dma_dummy_tx;
  112. dma_addr_t dma_dummy_rx;
  113. /* functions to deal with different sized buffers */
  114. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  115. u32(*get_tx) (struct mpc8xxx_spi *);
  116. unsigned int count;
  117. unsigned int irq;
  118. unsigned nsecs; /* (clock cycle time)/2 */
  119. u32 spibrg; /* SPIBRG input clock */
  120. u32 rx_shift; /* RX data reg shift when in qe mode */
  121. u32 tx_shift; /* TX data reg shift when in qe mode */
  122. unsigned int flags;
  123. struct workqueue_struct *workqueue;
  124. struct work_struct work;
  125. struct list_head queue;
  126. spinlock_t lock;
  127. struct completion done;
  128. };
  129. static void *mpc8xxx_dummy_rx;
  130. static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
  131. static int mpc8xxx_dummy_rx_refcnt;
  132. struct spi_mpc8xxx_cs {
  133. /* functions to deal with different sized buffers */
  134. void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
  135. u32 (*get_tx) (struct mpc8xxx_spi *);
  136. u32 rx_shift; /* RX data reg shift when in qe mode */
  137. u32 tx_shift; /* TX data reg shift when in qe mode */
  138. u32 hw_mode; /* Holds HW mode register settings */
  139. };
  140. static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
  141. {
  142. out_be32(reg, val);
  143. }
  144. static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
  145. {
  146. return in_be32(reg);
  147. }
  148. #define MPC83XX_SPI_RX_BUF(type) \
  149. static \
  150. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  151. { \
  152. type *rx = mpc8xxx_spi->rx; \
  153. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  154. mpc8xxx_spi->rx = rx; \
  155. }
  156. #define MPC83XX_SPI_TX_BUF(type) \
  157. static \
  158. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  159. { \
  160. u32 data; \
  161. const type *tx = mpc8xxx_spi->tx; \
  162. if (!tx) \
  163. return 0; \
  164. data = *tx++ << mpc8xxx_spi->tx_shift; \
  165. mpc8xxx_spi->tx = tx; \
  166. return data; \
  167. }
  168. MPC83XX_SPI_RX_BUF(u8)
  169. MPC83XX_SPI_RX_BUF(u16)
  170. MPC83XX_SPI_RX_BUF(u32)
  171. MPC83XX_SPI_TX_BUF(u8)
  172. MPC83XX_SPI_TX_BUF(u16)
  173. MPC83XX_SPI_TX_BUF(u32)
  174. static void mpc8xxx_spi_change_mode(struct spi_device *spi)
  175. {
  176. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  177. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  178. __be32 __iomem *mode = &mspi->base->mode;
  179. unsigned long flags;
  180. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  181. return;
  182. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  183. local_irq_save(flags);
  184. /* Turn off SPI unit prior changing mode */
  185. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  186. /* When in CPM mode, we need to reinit tx and rx. */
  187. if (mspi->flags & SPI_CPM_MODE) {
  188. if (mspi->flags & SPI_QE) {
  189. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  190. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  191. } else {
  192. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  193. if (mspi->flags & SPI_CPM1) {
  194. out_be16(&mspi->pram->rbptr,
  195. in_be16(&mspi->pram->rbase));
  196. out_be16(&mspi->pram->tbptr,
  197. in_be16(&mspi->pram->tbase));
  198. }
  199. }
  200. }
  201. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  202. local_irq_restore(flags);
  203. }
  204. static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
  205. {
  206. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  207. struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
  208. bool pol = spi->mode & SPI_CS_HIGH;
  209. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  210. if (value == BITBANG_CS_INACTIVE) {
  211. if (pdata->cs_control)
  212. pdata->cs_control(spi, !pol);
  213. }
  214. if (value == BITBANG_CS_ACTIVE) {
  215. mpc8xxx_spi->rx_shift = cs->rx_shift;
  216. mpc8xxx_spi->tx_shift = cs->tx_shift;
  217. mpc8xxx_spi->get_rx = cs->get_rx;
  218. mpc8xxx_spi->get_tx = cs->get_tx;
  219. mpc8xxx_spi_change_mode(spi);
  220. if (pdata->cs_control)
  221. pdata->cs_control(spi, pol);
  222. }
  223. }
  224. static int
  225. mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  226. struct spi_device *spi,
  227. struct mpc8xxx_spi *mpc8xxx_spi,
  228. int bits_per_word)
  229. {
  230. cs->rx_shift = 0;
  231. cs->tx_shift = 0;
  232. if (bits_per_word <= 8) {
  233. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  234. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  235. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  236. cs->rx_shift = 16;
  237. cs->tx_shift = 24;
  238. }
  239. } else if (bits_per_word <= 16) {
  240. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  241. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  242. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  243. cs->rx_shift = 16;
  244. cs->tx_shift = 16;
  245. }
  246. } else if (bits_per_word <= 32) {
  247. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  248. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  249. } else
  250. return -EINVAL;
  251. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  252. spi->mode & SPI_LSB_FIRST) {
  253. cs->tx_shift = 0;
  254. if (bits_per_word <= 8)
  255. cs->rx_shift = 8;
  256. else
  257. cs->rx_shift = 0;
  258. }
  259. mpc8xxx_spi->rx_shift = cs->rx_shift;
  260. mpc8xxx_spi->tx_shift = cs->tx_shift;
  261. mpc8xxx_spi->get_rx = cs->get_rx;
  262. mpc8xxx_spi->get_tx = cs->get_tx;
  263. return bits_per_word;
  264. }
  265. static int
  266. mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  267. struct spi_device *spi,
  268. int bits_per_word)
  269. {
  270. /* QE uses Little Endian for words > 8
  271. * so transform all words > 8 into 8 bits
  272. * Unfortnatly that doesn't work for LSB so
  273. * reject these for now */
  274. /* Note: 32 bits word, LSB works iff
  275. * tfcr/rfcr is set to CPMFCR_GBL */
  276. if (spi->mode & SPI_LSB_FIRST &&
  277. bits_per_word > 8)
  278. return -EINVAL;
  279. if (bits_per_word > 8)
  280. return 8; /* pretend its 8 bits */
  281. return bits_per_word;
  282. }
  283. static
  284. int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  285. {
  286. struct mpc8xxx_spi *mpc8xxx_spi;
  287. int bits_per_word;
  288. u8 pm;
  289. u32 hz;
  290. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  291. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  292. if (t) {
  293. bits_per_word = t->bits_per_word;
  294. hz = t->speed_hz;
  295. } else {
  296. bits_per_word = 0;
  297. hz = 0;
  298. }
  299. /* spi_transfer level calls that work per-word */
  300. if (!bits_per_word)
  301. bits_per_word = spi->bits_per_word;
  302. /* Make sure its a bit width we support [4..16, 32] */
  303. if ((bits_per_word < 4)
  304. || ((bits_per_word > 16) && (bits_per_word != 32)))
  305. return -EINVAL;
  306. if (!hz)
  307. hz = spi->max_speed_hz;
  308. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  309. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  310. mpc8xxx_spi,
  311. bits_per_word);
  312. else if (mpc8xxx_spi->flags & SPI_QE)
  313. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  314. bits_per_word);
  315. if (bits_per_word < 0)
  316. return bits_per_word;
  317. if (bits_per_word == 32)
  318. bits_per_word = 0;
  319. else
  320. bits_per_word = bits_per_word - 1;
  321. /* mask out bits we are going to set */
  322. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  323. | SPMODE_PM(0xF));
  324. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  325. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  326. cs->hw_mode |= SPMODE_DIV16;
  327. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  328. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  329. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  330. hz, mpc8xxx_spi->spibrg / 1024);
  331. if (pm > 16)
  332. pm = 16;
  333. } else
  334. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  335. if (pm)
  336. pm--;
  337. cs->hw_mode |= SPMODE_PM(pm);
  338. mpc8xxx_spi_change_mode(spi);
  339. return 0;
  340. }
  341. static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  342. {
  343. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  344. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  345. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  346. unsigned int xfer_ofs;
  347. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  348. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  349. out_be16(&rx_bd->cbd_datlen, 0);
  350. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  351. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  352. out_be16(&tx_bd->cbd_datlen, xfer_len);
  353. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  354. BD_SC_LAST);
  355. /* start transfer */
  356. mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
  357. }
  358. static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  359. struct spi_transfer *t, bool is_dma_mapped)
  360. {
  361. struct device *dev = mspi->dev;
  362. if (is_dma_mapped) {
  363. mspi->map_tx_dma = 0;
  364. mspi->map_rx_dma = 0;
  365. } else {
  366. mspi->map_tx_dma = 1;
  367. mspi->map_rx_dma = 1;
  368. }
  369. if (!t->tx_buf) {
  370. mspi->tx_dma = mspi->dma_dummy_tx;
  371. mspi->map_tx_dma = 0;
  372. }
  373. if (!t->rx_buf) {
  374. mspi->rx_dma = mspi->dma_dummy_rx;
  375. mspi->map_rx_dma = 0;
  376. }
  377. if (mspi->map_tx_dma) {
  378. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  379. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  380. DMA_TO_DEVICE);
  381. if (dma_mapping_error(dev, mspi->tx_dma)) {
  382. dev_err(dev, "unable to map tx dma\n");
  383. return -ENOMEM;
  384. }
  385. } else if (t->tx_buf) {
  386. mspi->tx_dma = t->tx_dma;
  387. }
  388. if (mspi->map_rx_dma) {
  389. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  390. DMA_FROM_DEVICE);
  391. if (dma_mapping_error(dev, mspi->rx_dma)) {
  392. dev_err(dev, "unable to map rx dma\n");
  393. goto err_rx_dma;
  394. }
  395. } else if (t->rx_buf) {
  396. mspi->rx_dma = t->rx_dma;
  397. }
  398. /* enable rx ints */
  399. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
  400. mspi->xfer_in_progress = t;
  401. mspi->count = t->len;
  402. /* start CPM transfers */
  403. mpc8xxx_spi_cpm_bufs_start(mspi);
  404. return 0;
  405. err_rx_dma:
  406. if (mspi->map_tx_dma)
  407. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  408. return -ENOMEM;
  409. }
  410. static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  411. {
  412. struct device *dev = mspi->dev;
  413. struct spi_transfer *t = mspi->xfer_in_progress;
  414. if (mspi->map_tx_dma)
  415. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  416. if (mspi->map_rx_dma)
  417. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  418. mspi->xfer_in_progress = NULL;
  419. }
  420. static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  421. struct spi_transfer *t, unsigned int len)
  422. {
  423. u32 word;
  424. mspi->count = len;
  425. /* enable rx ints */
  426. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
  427. /* transmit word */
  428. word = mspi->get_tx(mspi);
  429. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  430. return 0;
  431. }
  432. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  433. bool is_dma_mapped)
  434. {
  435. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  436. unsigned int len = t->len;
  437. u8 bits_per_word;
  438. int ret;
  439. bits_per_word = spi->bits_per_word;
  440. if (t->bits_per_word)
  441. bits_per_word = t->bits_per_word;
  442. if (bits_per_word > 8) {
  443. /* invalid length? */
  444. if (len & 1)
  445. return -EINVAL;
  446. len /= 2;
  447. }
  448. if (bits_per_word > 16) {
  449. /* invalid length? */
  450. if (len & 1)
  451. return -EINVAL;
  452. len /= 2;
  453. }
  454. mpc8xxx_spi->tx = t->tx_buf;
  455. mpc8xxx_spi->rx = t->rx_buf;
  456. INIT_COMPLETION(mpc8xxx_spi->done);
  457. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  458. ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  459. else
  460. ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
  461. if (ret)
  462. return ret;
  463. wait_for_completion(&mpc8xxx_spi->done);
  464. /* disable rx ints */
  465. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  466. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  467. mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
  468. return mpc8xxx_spi->count;
  469. }
  470. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  471. {
  472. struct spi_device *spi = m->spi;
  473. struct spi_transfer *t;
  474. unsigned int cs_change;
  475. const int nsecs = 50;
  476. int status;
  477. cs_change = 1;
  478. status = 0;
  479. list_for_each_entry(t, &m->transfers, transfer_list) {
  480. if (t->bits_per_word || t->speed_hz) {
  481. /* Don't allow changes if CS is active */
  482. status = -EINVAL;
  483. if (cs_change)
  484. status = mpc8xxx_spi_setup_transfer(spi, t);
  485. if (status < 0)
  486. break;
  487. }
  488. if (cs_change) {
  489. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  490. ndelay(nsecs);
  491. }
  492. cs_change = t->cs_change;
  493. if (t->len)
  494. status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
  495. if (status) {
  496. status = -EMSGSIZE;
  497. break;
  498. }
  499. m->actual_length += t->len;
  500. if (t->delay_usecs)
  501. udelay(t->delay_usecs);
  502. if (cs_change) {
  503. ndelay(nsecs);
  504. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  505. ndelay(nsecs);
  506. }
  507. }
  508. m->status = status;
  509. m->complete(m->context);
  510. if (status || !cs_change) {
  511. ndelay(nsecs);
  512. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  513. }
  514. mpc8xxx_spi_setup_transfer(spi, NULL);
  515. }
  516. static void mpc8xxx_spi_work(struct work_struct *work)
  517. {
  518. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  519. work);
  520. spin_lock_irq(&mpc8xxx_spi->lock);
  521. while (!list_empty(&mpc8xxx_spi->queue)) {
  522. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  523. struct spi_message, queue);
  524. list_del_init(&m->queue);
  525. spin_unlock_irq(&mpc8xxx_spi->lock);
  526. mpc8xxx_spi_do_one_msg(m);
  527. spin_lock_irq(&mpc8xxx_spi->lock);
  528. }
  529. spin_unlock_irq(&mpc8xxx_spi->lock);
  530. }
  531. static int mpc8xxx_spi_setup(struct spi_device *spi)
  532. {
  533. struct mpc8xxx_spi *mpc8xxx_spi;
  534. int retval;
  535. u32 hw_mode;
  536. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  537. if (!spi->max_speed_hz)
  538. return -EINVAL;
  539. if (!cs) {
  540. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  541. if (!cs)
  542. return -ENOMEM;
  543. spi->controller_state = cs;
  544. }
  545. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  546. hw_mode = cs->hw_mode; /* Save original settings */
  547. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  548. /* mask out bits we are going to set */
  549. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  550. | SPMODE_REV | SPMODE_LOOP);
  551. if (spi->mode & SPI_CPHA)
  552. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  553. if (spi->mode & SPI_CPOL)
  554. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  555. if (!(spi->mode & SPI_LSB_FIRST))
  556. cs->hw_mode |= SPMODE_REV;
  557. if (spi->mode & SPI_LOOP)
  558. cs->hw_mode |= SPMODE_LOOP;
  559. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  560. if (retval < 0) {
  561. cs->hw_mode = hw_mode; /* Restore settings */
  562. return retval;
  563. }
  564. return 0;
  565. }
  566. static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  567. {
  568. u16 len;
  569. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  570. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  571. len = in_be16(&mspi->rx_bd->cbd_datlen);
  572. if (len > mspi->count) {
  573. WARN_ON(1);
  574. len = mspi->count;
  575. }
  576. /* Clear the events */
  577. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  578. mspi->count -= len;
  579. if (mspi->count)
  580. mpc8xxx_spi_cpm_bufs_start(mspi);
  581. else
  582. complete(&mspi->done);
  583. }
  584. static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  585. {
  586. /* We need handle RX first */
  587. if (events & SPIE_NE) {
  588. u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
  589. if (mspi->rx)
  590. mspi->get_rx(rx_data, mspi);
  591. }
  592. if ((events & SPIE_NF) == 0)
  593. /* spin until TX is done */
  594. while (((events =
  595. mpc8xxx_spi_read_reg(&mspi->base->event)) &
  596. SPIE_NF) == 0)
  597. cpu_relax();
  598. /* Clear the events */
  599. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  600. mspi->count -= 1;
  601. if (mspi->count) {
  602. u32 word = mspi->get_tx(mspi);
  603. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  604. } else {
  605. complete(&mspi->done);
  606. }
  607. }
  608. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  609. {
  610. struct mpc8xxx_spi *mspi = context_data;
  611. irqreturn_t ret = IRQ_NONE;
  612. u32 events;
  613. /* Get interrupt events(tx/rx) */
  614. events = mpc8xxx_spi_read_reg(&mspi->base->event);
  615. if (events)
  616. ret = IRQ_HANDLED;
  617. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  618. if (mspi->flags & SPI_CPM_MODE)
  619. mpc8xxx_spi_cpm_irq(mspi, events);
  620. else
  621. mpc8xxx_spi_cpu_irq(mspi, events);
  622. return ret;
  623. }
  624. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  625. struct spi_message *m)
  626. {
  627. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  628. unsigned long flags;
  629. m->actual_length = 0;
  630. m->status = -EINPROGRESS;
  631. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  632. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  633. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  634. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  635. return 0;
  636. }
  637. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  638. {
  639. kfree(spi->controller_state);
  640. }
  641. static void *mpc8xxx_spi_alloc_dummy_rx(void)
  642. {
  643. mutex_lock(&mpc8xxx_dummy_rx_lock);
  644. if (!mpc8xxx_dummy_rx)
  645. mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  646. if (mpc8xxx_dummy_rx)
  647. mpc8xxx_dummy_rx_refcnt++;
  648. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  649. return mpc8xxx_dummy_rx;
  650. }
  651. static void mpc8xxx_spi_free_dummy_rx(void)
  652. {
  653. mutex_lock(&mpc8xxx_dummy_rx_lock);
  654. switch (mpc8xxx_dummy_rx_refcnt) {
  655. case 0:
  656. WARN_ON(1);
  657. break;
  658. case 1:
  659. kfree(mpc8xxx_dummy_rx);
  660. mpc8xxx_dummy_rx = NULL;
  661. /* fall through */
  662. default:
  663. mpc8xxx_dummy_rx_refcnt--;
  664. break;
  665. }
  666. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  667. }
  668. static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  669. {
  670. struct device *dev = mspi->dev;
  671. struct device_node *np = dev->of_node;
  672. const u32 *iprop;
  673. int size;
  674. unsigned long spi_base_ofs;
  675. unsigned long pram_ofs = -ENOMEM;
  676. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  677. iprop = of_get_property(np, "reg", &size);
  678. /* QE with a fixed pram location? */
  679. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  680. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  681. /* QE but with a dynamic pram location? */
  682. if (mspi->flags & SPI_QE) {
  683. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  684. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  685. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  686. return pram_ofs;
  687. }
  688. /* CPM1 and CPM2 pram must be at a fixed addr. */
  689. if (!iprop || size != sizeof(*iprop) * 4)
  690. return -ENOMEM;
  691. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  692. if (IS_ERR_VALUE(spi_base_ofs))
  693. return -ENOMEM;
  694. if (mspi->flags & SPI_CPM2) {
  695. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  696. if (!IS_ERR_VALUE(pram_ofs)) {
  697. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  698. out_be16(spi_base, pram_ofs);
  699. }
  700. } else {
  701. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  702. u16 rpbase = in_be16(&pram->rpbase);
  703. /* Microcode relocation patch applied? */
  704. if (rpbase)
  705. pram_ofs = rpbase;
  706. else
  707. return spi_base_ofs;
  708. }
  709. cpm_muram_free(spi_base_ofs);
  710. return pram_ofs;
  711. }
  712. static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
  713. {
  714. struct device *dev = mspi->dev;
  715. struct device_node *np = dev->of_node;
  716. const u32 *iprop;
  717. int size;
  718. unsigned long pram_ofs;
  719. unsigned long bds_ofs;
  720. if (!(mspi->flags & SPI_CPM_MODE))
  721. return 0;
  722. if (!mpc8xxx_spi_alloc_dummy_rx())
  723. return -ENOMEM;
  724. if (mspi->flags & SPI_QE) {
  725. iprop = of_get_property(np, "cell-index", &size);
  726. if (iprop && size == sizeof(*iprop))
  727. mspi->subblock = *iprop;
  728. switch (mspi->subblock) {
  729. default:
  730. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  731. /* fall through */
  732. case 0:
  733. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  734. break;
  735. case 1:
  736. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  737. break;
  738. }
  739. }
  740. pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
  741. if (IS_ERR_VALUE(pram_ofs)) {
  742. dev_err(dev, "can't allocate spi parameter ram\n");
  743. goto err_pram;
  744. }
  745. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  746. sizeof(*mspi->rx_bd), 8);
  747. if (IS_ERR_VALUE(bds_ofs)) {
  748. dev_err(dev, "can't allocate bds\n");
  749. goto err_bds;
  750. }
  751. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  752. DMA_TO_DEVICE);
  753. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  754. dev_err(dev, "unable to map dummy tx buffer\n");
  755. goto err_dummy_tx;
  756. }
  757. mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
  758. DMA_FROM_DEVICE);
  759. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  760. dev_err(dev, "unable to map dummy rx buffer\n");
  761. goto err_dummy_rx;
  762. }
  763. mspi->pram = cpm_muram_addr(pram_ofs);
  764. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  765. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  766. /* Initialize parameter ram. */
  767. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  768. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  769. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  770. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  771. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  772. out_be32(&mspi->pram->rstate, 0);
  773. out_be32(&mspi->pram->rdp, 0);
  774. out_be16(&mspi->pram->rbptr, 0);
  775. out_be16(&mspi->pram->rbc, 0);
  776. out_be32(&mspi->pram->rxtmp, 0);
  777. out_be32(&mspi->pram->tstate, 0);
  778. out_be32(&mspi->pram->tdp, 0);
  779. out_be16(&mspi->pram->tbptr, 0);
  780. out_be16(&mspi->pram->tbc, 0);
  781. out_be32(&mspi->pram->txtmp, 0);
  782. return 0;
  783. err_dummy_rx:
  784. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  785. err_dummy_tx:
  786. cpm_muram_free(bds_ofs);
  787. err_bds:
  788. cpm_muram_free(pram_ofs);
  789. err_pram:
  790. mpc8xxx_spi_free_dummy_rx();
  791. return -ENOMEM;
  792. }
  793. static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
  794. {
  795. struct device *dev = mspi->dev;
  796. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  797. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  798. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  799. cpm_muram_free(cpm_muram_offset(mspi->pram));
  800. mpc8xxx_spi_free_dummy_rx();
  801. }
  802. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  803. {
  804. if (flags & SPI_QE_CPU_MODE) {
  805. return "QE CPU";
  806. } else if (flags & SPI_CPM_MODE) {
  807. if (flags & SPI_QE)
  808. return "QE";
  809. else if (flags & SPI_CPM2)
  810. return "CPM2";
  811. else
  812. return "CPM1";
  813. }
  814. return "CPU";
  815. }
  816. static struct spi_master * __devinit
  817. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  818. {
  819. struct fsl_spi_platform_data *pdata = dev->platform_data;
  820. struct spi_master *master;
  821. struct mpc8xxx_spi *mpc8xxx_spi;
  822. u32 regval;
  823. int ret = 0;
  824. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  825. if (master == NULL) {
  826. ret = -ENOMEM;
  827. goto err;
  828. }
  829. dev_set_drvdata(dev, master);
  830. /* the spi->mode bits understood by this driver: */
  831. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  832. | SPI_LSB_FIRST | SPI_LOOP;
  833. master->setup = mpc8xxx_spi_setup;
  834. master->transfer = mpc8xxx_spi_transfer;
  835. master->cleanup = mpc8xxx_spi_cleanup;
  836. master->dev.of_node = dev->of_node;
  837. mpc8xxx_spi = spi_master_get_devdata(master);
  838. mpc8xxx_spi->dev = dev;
  839. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  840. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  841. mpc8xxx_spi->flags = pdata->flags;
  842. mpc8xxx_spi->spibrg = pdata->sysclk;
  843. ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
  844. if (ret)
  845. goto err_cpm_init;
  846. mpc8xxx_spi->rx_shift = 0;
  847. mpc8xxx_spi->tx_shift = 0;
  848. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  849. mpc8xxx_spi->rx_shift = 16;
  850. mpc8xxx_spi->tx_shift = 24;
  851. }
  852. init_completion(&mpc8xxx_spi->done);
  853. mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
  854. if (mpc8xxx_spi->base == NULL) {
  855. ret = -ENOMEM;
  856. goto err_ioremap;
  857. }
  858. mpc8xxx_spi->irq = irq;
  859. /* Register for SPI Interrupt */
  860. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  861. 0, "mpc8xxx_spi", mpc8xxx_spi);
  862. if (ret != 0)
  863. goto unmap_io;
  864. master->bus_num = pdata->bus_num;
  865. master->num_chipselect = pdata->max_chipselect;
  866. /* SPI controller initializations */
  867. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  868. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  869. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  870. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  871. /* Enable SPI interface */
  872. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  873. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  874. regval |= SPMODE_OP;
  875. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  876. spin_lock_init(&mpc8xxx_spi->lock);
  877. init_completion(&mpc8xxx_spi->done);
  878. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  879. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  880. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  881. dev_name(master->dev.parent));
  882. if (mpc8xxx_spi->workqueue == NULL) {
  883. ret = -EBUSY;
  884. goto free_irq;
  885. }
  886. ret = spi_register_master(master);
  887. if (ret < 0)
  888. goto unreg_master;
  889. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  890. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  891. return master;
  892. unreg_master:
  893. destroy_workqueue(mpc8xxx_spi->workqueue);
  894. free_irq:
  895. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  896. unmap_io:
  897. iounmap(mpc8xxx_spi->base);
  898. err_ioremap:
  899. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  900. err_cpm_init:
  901. spi_master_put(master);
  902. err:
  903. return ERR_PTR(ret);
  904. }
  905. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  906. {
  907. struct mpc8xxx_spi *mpc8xxx_spi;
  908. struct spi_master *master;
  909. master = dev_get_drvdata(dev);
  910. mpc8xxx_spi = spi_master_get_devdata(master);
  911. flush_workqueue(mpc8xxx_spi->workqueue);
  912. destroy_workqueue(mpc8xxx_spi->workqueue);
  913. spi_unregister_master(master);
  914. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  915. iounmap(mpc8xxx_spi->base);
  916. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  917. return 0;
  918. }
  919. struct mpc8xxx_spi_probe_info {
  920. struct fsl_spi_platform_data pdata;
  921. int *gpios;
  922. bool *alow_flags;
  923. };
  924. static struct mpc8xxx_spi_probe_info *
  925. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  926. {
  927. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  928. }
  929. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  930. {
  931. struct device *dev = spi->dev.parent;
  932. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  933. u16 cs = spi->chip_select;
  934. int gpio = pinfo->gpios[cs];
  935. bool alow = pinfo->alow_flags[cs];
  936. gpio_set_value(gpio, on ^ alow);
  937. }
  938. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  939. {
  940. struct device_node *np = dev->of_node;
  941. struct fsl_spi_platform_data *pdata = dev->platform_data;
  942. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  943. unsigned int ngpios;
  944. int i = 0;
  945. int ret;
  946. ngpios = of_gpio_count(np);
  947. if (!ngpios) {
  948. /*
  949. * SPI w/o chip-select line. One SPI device is still permitted
  950. * though.
  951. */
  952. pdata->max_chipselect = 1;
  953. return 0;
  954. }
  955. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  956. if (!pinfo->gpios)
  957. return -ENOMEM;
  958. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  959. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  960. GFP_KERNEL);
  961. if (!pinfo->alow_flags) {
  962. ret = -ENOMEM;
  963. goto err_alloc_flags;
  964. }
  965. for (; i < ngpios; i++) {
  966. int gpio;
  967. enum of_gpio_flags flags;
  968. gpio = of_get_gpio_flags(np, i, &flags);
  969. if (!gpio_is_valid(gpio)) {
  970. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  971. ret = gpio;
  972. goto err_loop;
  973. }
  974. ret = gpio_request(gpio, dev_name(dev));
  975. if (ret) {
  976. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  977. goto err_loop;
  978. }
  979. pinfo->gpios[i] = gpio;
  980. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  981. ret = gpio_direction_output(pinfo->gpios[i],
  982. pinfo->alow_flags[i]);
  983. if (ret) {
  984. dev_err(dev, "can't set output direction for gpio "
  985. "#%d: %d\n", i, ret);
  986. goto err_loop;
  987. }
  988. }
  989. pdata->max_chipselect = ngpios;
  990. pdata->cs_control = mpc8xxx_spi_cs_control;
  991. return 0;
  992. err_loop:
  993. while (i >= 0) {
  994. if (gpio_is_valid(pinfo->gpios[i]))
  995. gpio_free(pinfo->gpios[i]);
  996. i--;
  997. }
  998. kfree(pinfo->alow_flags);
  999. pinfo->alow_flags = NULL;
  1000. err_alloc_flags:
  1001. kfree(pinfo->gpios);
  1002. pinfo->gpios = NULL;
  1003. return ret;
  1004. }
  1005. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  1006. {
  1007. struct fsl_spi_platform_data *pdata = dev->platform_data;
  1008. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  1009. int i;
  1010. if (!pinfo->gpios)
  1011. return 0;
  1012. for (i = 0; i < pdata->max_chipselect; i++) {
  1013. if (gpio_is_valid(pinfo->gpios[i]))
  1014. gpio_free(pinfo->gpios[i]);
  1015. }
  1016. kfree(pinfo->gpios);
  1017. kfree(pinfo->alow_flags);
  1018. return 0;
  1019. }
  1020. static int __devinit of_mpc8xxx_spi_probe(struct platform_device *ofdev,
  1021. const struct of_device_id *ofid)
  1022. {
  1023. struct device *dev = &ofdev->dev;
  1024. struct device_node *np = ofdev->dev.of_node;
  1025. struct mpc8xxx_spi_probe_info *pinfo;
  1026. struct fsl_spi_platform_data *pdata;
  1027. struct spi_master *master;
  1028. struct resource mem;
  1029. struct resource irq;
  1030. const void *prop;
  1031. int ret = -ENOMEM;
  1032. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  1033. if (!pinfo)
  1034. return -ENOMEM;
  1035. pdata = &pinfo->pdata;
  1036. dev->platform_data = pdata;
  1037. /* Allocate bus num dynamically. */
  1038. pdata->bus_num = -1;
  1039. /* SPI controller is either clocked from QE or SoC clock. */
  1040. pdata->sysclk = get_brgfreq();
  1041. if (pdata->sysclk == -1) {
  1042. pdata->sysclk = fsl_get_sys_freq();
  1043. if (pdata->sysclk == -1) {
  1044. ret = -ENODEV;
  1045. goto err_clk;
  1046. }
  1047. }
  1048. prop = of_get_property(np, "mode", NULL);
  1049. if (prop && !strcmp(prop, "cpu-qe"))
  1050. pdata->flags = SPI_QE_CPU_MODE;
  1051. else if (prop && !strcmp(prop, "qe"))
  1052. pdata->flags = SPI_CPM_MODE | SPI_QE;
  1053. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  1054. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  1055. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  1056. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  1057. ret = of_mpc8xxx_spi_get_chipselects(dev);
  1058. if (ret)
  1059. goto err;
  1060. ret = of_address_to_resource(np, 0, &mem);
  1061. if (ret)
  1062. goto err;
  1063. ret = of_irq_to_resource(np, 0, &irq);
  1064. if (!ret) {
  1065. ret = -EINVAL;
  1066. goto err;
  1067. }
  1068. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  1069. if (IS_ERR(master)) {
  1070. ret = PTR_ERR(master);
  1071. goto err;
  1072. }
  1073. return 0;
  1074. err:
  1075. of_mpc8xxx_spi_free_chipselects(dev);
  1076. err_clk:
  1077. kfree(pinfo);
  1078. return ret;
  1079. }
  1080. static int __devexit of_mpc8xxx_spi_remove(struct platform_device *ofdev)
  1081. {
  1082. int ret;
  1083. ret = mpc8xxx_spi_remove(&ofdev->dev);
  1084. if (ret)
  1085. return ret;
  1086. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  1087. return 0;
  1088. }
  1089. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  1090. { .compatible = "fsl,spi" },
  1091. {},
  1092. };
  1093. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  1094. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  1095. .driver = {
  1096. .name = "mpc8xxx_spi",
  1097. .owner = THIS_MODULE,
  1098. .of_match_table = of_mpc8xxx_spi_match,
  1099. },
  1100. .probe = of_mpc8xxx_spi_probe,
  1101. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  1102. };
  1103. #ifdef CONFIG_MPC832x_RDB
  1104. /*
  1105. * XXX XXX XXX
  1106. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  1107. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  1108. * tree can work with OpenFirmware driver. But for now we support old trees
  1109. * as well.
  1110. */
  1111. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  1112. {
  1113. struct resource *mem;
  1114. int irq;
  1115. struct spi_master *master;
  1116. if (!pdev->dev.platform_data)
  1117. return -EINVAL;
  1118. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1119. if (!mem)
  1120. return -EINVAL;
  1121. irq = platform_get_irq(pdev, 0);
  1122. if (irq <= 0)
  1123. return -EINVAL;
  1124. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  1125. if (IS_ERR(master))
  1126. return PTR_ERR(master);
  1127. return 0;
  1128. }
  1129. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  1130. {
  1131. return mpc8xxx_spi_remove(&pdev->dev);
  1132. }
  1133. MODULE_ALIAS("platform:mpc8xxx_spi");
  1134. static struct platform_driver mpc8xxx_spi_driver = {
  1135. .probe = plat_mpc8xxx_spi_probe,
  1136. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  1137. .driver = {
  1138. .name = "mpc8xxx_spi",
  1139. .owner = THIS_MODULE,
  1140. },
  1141. };
  1142. static bool legacy_driver_failed;
  1143. static void __init legacy_driver_register(void)
  1144. {
  1145. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  1146. }
  1147. static void __exit legacy_driver_unregister(void)
  1148. {
  1149. if (legacy_driver_failed)
  1150. return;
  1151. platform_driver_unregister(&mpc8xxx_spi_driver);
  1152. }
  1153. #else
  1154. static void __init legacy_driver_register(void) {}
  1155. static void __exit legacy_driver_unregister(void) {}
  1156. #endif /* CONFIG_MPC832x_RDB */
  1157. static int __init mpc8xxx_spi_init(void)
  1158. {
  1159. legacy_driver_register();
  1160. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  1161. }
  1162. static void __exit mpc8xxx_spi_exit(void)
  1163. {
  1164. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  1165. legacy_driver_unregister();
  1166. }
  1167. module_init(mpc8xxx_spi_init);
  1168. module_exit(mpc8xxx_spi_exit);
  1169. MODULE_AUTHOR("Kumar Gala");
  1170. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  1171. MODULE_LICENSE("GPL");