spi_mpc8xxx.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. if (mspi->rx_dma == mspi->dma_dummy_rx)
  349. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma);
  350. else
  351. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  352. out_be16(&rx_bd->cbd_datlen, 0);
  353. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  354. if (mspi->tx_dma == mspi->dma_dummy_tx)
  355. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma);
  356. else
  357. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  358. out_be16(&tx_bd->cbd_datlen, xfer_len);
  359. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  360. BD_SC_LAST);
  361. /* start transfer */
  362. mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
  363. }
  364. static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  365. struct spi_transfer *t, bool is_dma_mapped)
  366. {
  367. struct device *dev = mspi->dev;
  368. if (is_dma_mapped) {
  369. mspi->map_tx_dma = 0;
  370. mspi->map_rx_dma = 0;
  371. } else {
  372. mspi->map_tx_dma = 1;
  373. mspi->map_rx_dma = 1;
  374. }
  375. if (!t->tx_buf) {
  376. mspi->tx_dma = mspi->dma_dummy_tx;
  377. mspi->map_tx_dma = 0;
  378. }
  379. if (!t->rx_buf) {
  380. mspi->rx_dma = mspi->dma_dummy_rx;
  381. mspi->map_rx_dma = 0;
  382. }
  383. if (mspi->map_tx_dma) {
  384. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  385. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  386. DMA_TO_DEVICE);
  387. if (dma_mapping_error(dev, mspi->tx_dma)) {
  388. dev_err(dev, "unable to map tx dma\n");
  389. return -ENOMEM;
  390. }
  391. } else if (t->tx_buf) {
  392. mspi->tx_dma = t->tx_dma;
  393. }
  394. if (mspi->map_rx_dma) {
  395. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  396. DMA_FROM_DEVICE);
  397. if (dma_mapping_error(dev, mspi->rx_dma)) {
  398. dev_err(dev, "unable to map rx dma\n");
  399. goto err_rx_dma;
  400. }
  401. } else if (t->rx_buf) {
  402. mspi->rx_dma = t->rx_dma;
  403. }
  404. /* enable rx ints */
  405. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
  406. mspi->xfer_in_progress = t;
  407. mspi->count = t->len;
  408. /* start CPM transfers */
  409. mpc8xxx_spi_cpm_bufs_start(mspi);
  410. return 0;
  411. err_rx_dma:
  412. if (mspi->map_tx_dma)
  413. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  414. return -ENOMEM;
  415. }
  416. static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  417. {
  418. struct device *dev = mspi->dev;
  419. struct spi_transfer *t = mspi->xfer_in_progress;
  420. if (mspi->map_tx_dma)
  421. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  422. if (mspi->map_rx_dma)
  423. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  424. mspi->xfer_in_progress = NULL;
  425. }
  426. static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  427. struct spi_transfer *t, unsigned int len)
  428. {
  429. u32 word;
  430. mspi->count = len;
  431. /* enable rx ints */
  432. mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
  433. /* transmit word */
  434. word = mspi->get_tx(mspi);
  435. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  436. return 0;
  437. }
  438. static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  439. bool is_dma_mapped)
  440. {
  441. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  442. unsigned int len = t->len;
  443. u8 bits_per_word;
  444. int ret;
  445. bits_per_word = spi->bits_per_word;
  446. if (t->bits_per_word)
  447. bits_per_word = t->bits_per_word;
  448. if (bits_per_word > 8) {
  449. /* invalid length? */
  450. if (len & 1)
  451. return -EINVAL;
  452. len /= 2;
  453. }
  454. if (bits_per_word > 16) {
  455. /* invalid length? */
  456. if (len & 1)
  457. return -EINVAL;
  458. len /= 2;
  459. }
  460. mpc8xxx_spi->tx = t->tx_buf;
  461. mpc8xxx_spi->rx = t->rx_buf;
  462. INIT_COMPLETION(mpc8xxx_spi->done);
  463. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  464. ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  465. else
  466. ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
  467. if (ret)
  468. return ret;
  469. wait_for_completion(&mpc8xxx_spi->done);
  470. /* disable rx ints */
  471. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  472. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  473. mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
  474. return mpc8xxx_spi->count;
  475. }
  476. static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
  477. {
  478. struct spi_device *spi = m->spi;
  479. struct spi_transfer *t;
  480. unsigned int cs_change;
  481. const int nsecs = 50;
  482. int status;
  483. cs_change = 1;
  484. status = 0;
  485. list_for_each_entry(t, &m->transfers, transfer_list) {
  486. if (t->bits_per_word || t->speed_hz) {
  487. /* Don't allow changes if CS is active */
  488. status = -EINVAL;
  489. if (cs_change)
  490. status = mpc8xxx_spi_setup_transfer(spi, t);
  491. if (status < 0)
  492. break;
  493. }
  494. if (cs_change) {
  495. mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  496. ndelay(nsecs);
  497. }
  498. cs_change = t->cs_change;
  499. if (t->len)
  500. status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
  501. if (status) {
  502. status = -EMSGSIZE;
  503. break;
  504. }
  505. m->actual_length += t->len;
  506. if (t->delay_usecs)
  507. udelay(t->delay_usecs);
  508. if (cs_change) {
  509. ndelay(nsecs);
  510. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  511. ndelay(nsecs);
  512. }
  513. }
  514. m->status = status;
  515. m->complete(m->context);
  516. if (status || !cs_change) {
  517. ndelay(nsecs);
  518. mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  519. }
  520. mpc8xxx_spi_setup_transfer(spi, NULL);
  521. }
  522. static void mpc8xxx_spi_work(struct work_struct *work)
  523. {
  524. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  525. work);
  526. spin_lock_irq(&mpc8xxx_spi->lock);
  527. while (!list_empty(&mpc8xxx_spi->queue)) {
  528. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  529. struct spi_message, queue);
  530. list_del_init(&m->queue);
  531. spin_unlock_irq(&mpc8xxx_spi->lock);
  532. mpc8xxx_spi_do_one_msg(m);
  533. spin_lock_irq(&mpc8xxx_spi->lock);
  534. }
  535. spin_unlock_irq(&mpc8xxx_spi->lock);
  536. }
  537. static int mpc8xxx_spi_setup(struct spi_device *spi)
  538. {
  539. struct mpc8xxx_spi *mpc8xxx_spi;
  540. int retval;
  541. u32 hw_mode;
  542. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  543. if (!spi->max_speed_hz)
  544. return -EINVAL;
  545. if (!cs) {
  546. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  547. if (!cs)
  548. return -ENOMEM;
  549. spi->controller_state = cs;
  550. }
  551. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  552. hw_mode = cs->hw_mode; /* Save original settings */
  553. cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
  554. /* mask out bits we are going to set */
  555. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  556. | SPMODE_REV | SPMODE_LOOP);
  557. if (spi->mode & SPI_CPHA)
  558. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  559. if (spi->mode & SPI_CPOL)
  560. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  561. if (!(spi->mode & SPI_LSB_FIRST))
  562. cs->hw_mode |= SPMODE_REV;
  563. if (spi->mode & SPI_LOOP)
  564. cs->hw_mode |= SPMODE_LOOP;
  565. retval = mpc8xxx_spi_setup_transfer(spi, NULL);
  566. if (retval < 0) {
  567. cs->hw_mode = hw_mode; /* Restore settings */
  568. return retval;
  569. }
  570. return 0;
  571. }
  572. static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  573. {
  574. u16 len;
  575. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  576. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  577. len = in_be16(&mspi->rx_bd->cbd_datlen);
  578. if (len > mspi->count) {
  579. WARN_ON(1);
  580. len = mspi->count;
  581. }
  582. /* Clear the events */
  583. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  584. mspi->count -= len;
  585. if (mspi->count)
  586. mpc8xxx_spi_cpm_bufs_start(mspi);
  587. else
  588. complete(&mspi->done);
  589. }
  590. static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  591. {
  592. /* We need handle RX first */
  593. if (events & SPIE_NE) {
  594. u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
  595. if (mspi->rx)
  596. mspi->get_rx(rx_data, mspi);
  597. }
  598. if ((events & SPIE_NF) == 0)
  599. /* spin until TX is done */
  600. while (((events =
  601. mpc8xxx_spi_read_reg(&mspi->base->event)) &
  602. SPIE_NF) == 0)
  603. cpu_relax();
  604. /* Clear the events */
  605. mpc8xxx_spi_write_reg(&mspi->base->event, events);
  606. mspi->count -= 1;
  607. if (mspi->count) {
  608. u32 word = mspi->get_tx(mspi);
  609. mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
  610. } else {
  611. complete(&mspi->done);
  612. }
  613. }
  614. static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
  615. {
  616. struct mpc8xxx_spi *mspi = context_data;
  617. irqreturn_t ret = IRQ_NONE;
  618. u32 events;
  619. /* Get interrupt events(tx/rx) */
  620. events = mpc8xxx_spi_read_reg(&mspi->base->event);
  621. if (events)
  622. ret = IRQ_HANDLED;
  623. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  624. if (mspi->flags & SPI_CPM_MODE)
  625. mpc8xxx_spi_cpm_irq(mspi, events);
  626. else
  627. mpc8xxx_spi_cpu_irq(mspi, events);
  628. return ret;
  629. }
  630. static int mpc8xxx_spi_transfer(struct spi_device *spi,
  631. struct spi_message *m)
  632. {
  633. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  634. unsigned long flags;
  635. m->actual_length = 0;
  636. m->status = -EINPROGRESS;
  637. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  638. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  639. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  640. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  641. return 0;
  642. }
  643. static void mpc8xxx_spi_cleanup(struct spi_device *spi)
  644. {
  645. kfree(spi->controller_state);
  646. }
  647. static void *mpc8xxx_spi_alloc_dummy_rx(void)
  648. {
  649. mutex_lock(&mpc8xxx_dummy_rx_lock);
  650. if (!mpc8xxx_dummy_rx)
  651. mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  652. if (mpc8xxx_dummy_rx)
  653. mpc8xxx_dummy_rx_refcnt++;
  654. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  655. return mpc8xxx_dummy_rx;
  656. }
  657. static void mpc8xxx_spi_free_dummy_rx(void)
  658. {
  659. mutex_lock(&mpc8xxx_dummy_rx_lock);
  660. switch (mpc8xxx_dummy_rx_refcnt) {
  661. case 0:
  662. WARN_ON(1);
  663. break;
  664. case 1:
  665. kfree(mpc8xxx_dummy_rx);
  666. mpc8xxx_dummy_rx = NULL;
  667. /* fall through */
  668. default:
  669. mpc8xxx_dummy_rx_refcnt--;
  670. break;
  671. }
  672. mutex_unlock(&mpc8xxx_dummy_rx_lock);
  673. }
  674. static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  675. {
  676. struct device *dev = mspi->dev;
  677. struct device_node *np = dev->of_node;
  678. const u32 *iprop;
  679. int size;
  680. unsigned long spi_base_ofs;
  681. unsigned long pram_ofs = -ENOMEM;
  682. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  683. iprop = of_get_property(np, "reg", &size);
  684. /* QE with a fixed pram location? */
  685. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  686. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  687. /* QE but with a dynamic pram location? */
  688. if (mspi->flags & SPI_QE) {
  689. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  690. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  691. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  692. return pram_ofs;
  693. }
  694. /* CPM1 and CPM2 pram must be at a fixed addr. */
  695. if (!iprop || size != sizeof(*iprop) * 4)
  696. return -ENOMEM;
  697. spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
  698. if (IS_ERR_VALUE(spi_base_ofs))
  699. return -ENOMEM;
  700. if (mspi->flags & SPI_CPM2) {
  701. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  702. if (!IS_ERR_VALUE(pram_ofs)) {
  703. u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
  704. out_be16(spi_base, pram_ofs);
  705. }
  706. } else {
  707. struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
  708. u16 rpbase = in_be16(&pram->rpbase);
  709. /* Microcode relocation patch applied? */
  710. if (rpbase)
  711. pram_ofs = rpbase;
  712. else
  713. return spi_base_ofs;
  714. }
  715. cpm_muram_free(spi_base_ofs);
  716. return pram_ofs;
  717. }
  718. static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
  719. {
  720. struct device *dev = mspi->dev;
  721. struct device_node *np = dev->of_node;
  722. const u32 *iprop;
  723. int size;
  724. unsigned long pram_ofs;
  725. unsigned long bds_ofs;
  726. if (!(mspi->flags & SPI_CPM_MODE))
  727. return 0;
  728. if (!mpc8xxx_spi_alloc_dummy_rx())
  729. return -ENOMEM;
  730. if (mspi->flags & SPI_QE) {
  731. iprop = of_get_property(np, "cell-index", &size);
  732. if (iprop && size == sizeof(*iprop))
  733. mspi->subblock = *iprop;
  734. switch (mspi->subblock) {
  735. default:
  736. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  737. /* fall through */
  738. case 0:
  739. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  740. break;
  741. case 1:
  742. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  743. break;
  744. }
  745. }
  746. pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
  747. if (IS_ERR_VALUE(pram_ofs)) {
  748. dev_err(dev, "can't allocate spi parameter ram\n");
  749. goto err_pram;
  750. }
  751. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  752. sizeof(*mspi->rx_bd), 8);
  753. if (IS_ERR_VALUE(bds_ofs)) {
  754. dev_err(dev, "can't allocate bds\n");
  755. goto err_bds;
  756. }
  757. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  758. DMA_TO_DEVICE);
  759. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  760. dev_err(dev, "unable to map dummy tx buffer\n");
  761. goto err_dummy_tx;
  762. }
  763. mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
  764. DMA_FROM_DEVICE);
  765. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  766. dev_err(dev, "unable to map dummy rx buffer\n");
  767. goto err_dummy_rx;
  768. }
  769. mspi->pram = cpm_muram_addr(pram_ofs);
  770. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  771. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  772. /* Initialize parameter ram. */
  773. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  774. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  775. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  776. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  777. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  778. out_be32(&mspi->pram->rstate, 0);
  779. out_be32(&mspi->pram->rdp, 0);
  780. out_be16(&mspi->pram->rbptr, 0);
  781. out_be16(&mspi->pram->rbc, 0);
  782. out_be32(&mspi->pram->rxtmp, 0);
  783. out_be32(&mspi->pram->tstate, 0);
  784. out_be32(&mspi->pram->tdp, 0);
  785. out_be16(&mspi->pram->tbptr, 0);
  786. out_be16(&mspi->pram->tbc, 0);
  787. out_be32(&mspi->pram->txtmp, 0);
  788. return 0;
  789. err_dummy_rx:
  790. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  791. err_dummy_tx:
  792. cpm_muram_free(bds_ofs);
  793. err_bds:
  794. cpm_muram_free(pram_ofs);
  795. err_pram:
  796. mpc8xxx_spi_free_dummy_rx();
  797. return -ENOMEM;
  798. }
  799. static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
  800. {
  801. struct device *dev = mspi->dev;
  802. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  803. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  804. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  805. cpm_muram_free(cpm_muram_offset(mspi->pram));
  806. mpc8xxx_spi_free_dummy_rx();
  807. }
  808. static const char *mpc8xxx_spi_strmode(unsigned int flags)
  809. {
  810. if (flags & SPI_QE_CPU_MODE) {
  811. return "QE CPU";
  812. } else if (flags & SPI_CPM_MODE) {
  813. if (flags & SPI_QE)
  814. return "QE";
  815. else if (flags & SPI_CPM2)
  816. return "CPM2";
  817. else
  818. return "CPM1";
  819. }
  820. return "CPU";
  821. }
  822. static struct spi_master * __devinit
  823. mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
  824. {
  825. struct fsl_spi_platform_data *pdata = dev->platform_data;
  826. struct spi_master *master;
  827. struct mpc8xxx_spi *mpc8xxx_spi;
  828. u32 regval;
  829. int ret = 0;
  830. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  831. if (master == NULL) {
  832. ret = -ENOMEM;
  833. goto err;
  834. }
  835. dev_set_drvdata(dev, master);
  836. /* the spi->mode bits understood by this driver: */
  837. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  838. | SPI_LSB_FIRST | SPI_LOOP;
  839. master->setup = mpc8xxx_spi_setup;
  840. master->transfer = mpc8xxx_spi_transfer;
  841. master->cleanup = mpc8xxx_spi_cleanup;
  842. master->dev.of_node = dev->of_node;
  843. mpc8xxx_spi = spi_master_get_devdata(master);
  844. mpc8xxx_spi->dev = dev;
  845. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  846. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  847. mpc8xxx_spi->flags = pdata->flags;
  848. mpc8xxx_spi->spibrg = pdata->sysclk;
  849. ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
  850. if (ret)
  851. goto err_cpm_init;
  852. mpc8xxx_spi->rx_shift = 0;
  853. mpc8xxx_spi->tx_shift = 0;
  854. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  855. mpc8xxx_spi->rx_shift = 16;
  856. mpc8xxx_spi->tx_shift = 24;
  857. }
  858. init_completion(&mpc8xxx_spi->done);
  859. mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
  860. if (mpc8xxx_spi->base == NULL) {
  861. ret = -ENOMEM;
  862. goto err_ioremap;
  863. }
  864. mpc8xxx_spi->irq = irq;
  865. /* Register for SPI Interrupt */
  866. ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
  867. 0, "mpc8xxx_spi", mpc8xxx_spi);
  868. if (ret != 0)
  869. goto unmap_io;
  870. master->bus_num = pdata->bus_num;
  871. master->num_chipselect = pdata->max_chipselect;
  872. /* SPI controller initializations */
  873. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
  874. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
  875. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
  876. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
  877. /* Enable SPI interface */
  878. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  879. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  880. regval |= SPMODE_OP;
  881. mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
  882. spin_lock_init(&mpc8xxx_spi->lock);
  883. init_completion(&mpc8xxx_spi->done);
  884. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  885. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  886. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  887. dev_name(master->dev.parent));
  888. if (mpc8xxx_spi->workqueue == NULL) {
  889. ret = -EBUSY;
  890. goto free_irq;
  891. }
  892. ret = spi_register_master(master);
  893. if (ret < 0)
  894. goto unreg_master;
  895. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
  896. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  897. return master;
  898. unreg_master:
  899. destroy_workqueue(mpc8xxx_spi->workqueue);
  900. free_irq:
  901. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  902. unmap_io:
  903. iounmap(mpc8xxx_spi->base);
  904. err_ioremap:
  905. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  906. err_cpm_init:
  907. spi_master_put(master);
  908. err:
  909. return ERR_PTR(ret);
  910. }
  911. static int __devexit mpc8xxx_spi_remove(struct device *dev)
  912. {
  913. struct mpc8xxx_spi *mpc8xxx_spi;
  914. struct spi_master *master;
  915. master = dev_get_drvdata(dev);
  916. mpc8xxx_spi = spi_master_get_devdata(master);
  917. flush_workqueue(mpc8xxx_spi->workqueue);
  918. destroy_workqueue(mpc8xxx_spi->workqueue);
  919. spi_unregister_master(master);
  920. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  921. iounmap(mpc8xxx_spi->base);
  922. mpc8xxx_spi_cpm_free(mpc8xxx_spi);
  923. return 0;
  924. }
  925. struct mpc8xxx_spi_probe_info {
  926. struct fsl_spi_platform_data pdata;
  927. int *gpios;
  928. bool *alow_flags;
  929. };
  930. static struct mpc8xxx_spi_probe_info *
  931. to_of_pinfo(struct fsl_spi_platform_data *pdata)
  932. {
  933. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  934. }
  935. static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
  936. {
  937. struct device *dev = spi->dev.parent;
  938. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  939. u16 cs = spi->chip_select;
  940. int gpio = pinfo->gpios[cs];
  941. bool alow = pinfo->alow_flags[cs];
  942. gpio_set_value(gpio, on ^ alow);
  943. }
  944. static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
  945. {
  946. struct device_node *np = dev->of_node;
  947. struct fsl_spi_platform_data *pdata = dev->platform_data;
  948. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  949. unsigned int ngpios;
  950. int i = 0;
  951. int ret;
  952. ngpios = of_gpio_count(np);
  953. if (!ngpios) {
  954. /*
  955. * SPI w/o chip-select line. One SPI device is still permitted
  956. * though.
  957. */
  958. pdata->max_chipselect = 1;
  959. return 0;
  960. }
  961. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  962. if (!pinfo->gpios)
  963. return -ENOMEM;
  964. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  965. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  966. GFP_KERNEL);
  967. if (!pinfo->alow_flags) {
  968. ret = -ENOMEM;
  969. goto err_alloc_flags;
  970. }
  971. for (; i < ngpios; i++) {
  972. int gpio;
  973. enum of_gpio_flags flags;
  974. gpio = of_get_gpio_flags(np, i, &flags);
  975. if (!gpio_is_valid(gpio)) {
  976. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  977. ret = gpio;
  978. goto err_loop;
  979. }
  980. ret = gpio_request(gpio, dev_name(dev));
  981. if (ret) {
  982. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  983. goto err_loop;
  984. }
  985. pinfo->gpios[i] = gpio;
  986. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  987. ret = gpio_direction_output(pinfo->gpios[i],
  988. pinfo->alow_flags[i]);
  989. if (ret) {
  990. dev_err(dev, "can't set output direction for gpio "
  991. "#%d: %d\n", i, ret);
  992. goto err_loop;
  993. }
  994. }
  995. pdata->max_chipselect = ngpios;
  996. pdata->cs_control = mpc8xxx_spi_cs_control;
  997. return 0;
  998. err_loop:
  999. while (i >= 0) {
  1000. if (gpio_is_valid(pinfo->gpios[i]))
  1001. gpio_free(pinfo->gpios[i]);
  1002. i--;
  1003. }
  1004. kfree(pinfo->alow_flags);
  1005. pinfo->alow_flags = NULL;
  1006. err_alloc_flags:
  1007. kfree(pinfo->gpios);
  1008. pinfo->gpios = NULL;
  1009. return ret;
  1010. }
  1011. static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
  1012. {
  1013. struct fsl_spi_platform_data *pdata = dev->platform_data;
  1014. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  1015. int i;
  1016. if (!pinfo->gpios)
  1017. return 0;
  1018. for (i = 0; i < pdata->max_chipselect; i++) {
  1019. if (gpio_is_valid(pinfo->gpios[i]))
  1020. gpio_free(pinfo->gpios[i]);
  1021. }
  1022. kfree(pinfo->gpios);
  1023. kfree(pinfo->alow_flags);
  1024. return 0;
  1025. }
  1026. static int __devinit of_mpc8xxx_spi_probe(struct platform_device *ofdev,
  1027. const struct of_device_id *ofid)
  1028. {
  1029. struct device *dev = &ofdev->dev;
  1030. struct device_node *np = ofdev->dev.of_node;
  1031. struct mpc8xxx_spi_probe_info *pinfo;
  1032. struct fsl_spi_platform_data *pdata;
  1033. struct spi_master *master;
  1034. struct resource mem;
  1035. struct resource irq;
  1036. const void *prop;
  1037. int ret = -ENOMEM;
  1038. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  1039. if (!pinfo)
  1040. return -ENOMEM;
  1041. pdata = &pinfo->pdata;
  1042. dev->platform_data = pdata;
  1043. /* Allocate bus num dynamically. */
  1044. pdata->bus_num = -1;
  1045. /* SPI controller is either clocked from QE or SoC clock. */
  1046. pdata->sysclk = get_brgfreq();
  1047. if (pdata->sysclk == -1) {
  1048. pdata->sysclk = fsl_get_sys_freq();
  1049. if (pdata->sysclk == -1) {
  1050. ret = -ENODEV;
  1051. goto err_clk;
  1052. }
  1053. }
  1054. prop = of_get_property(np, "mode", NULL);
  1055. if (prop && !strcmp(prop, "cpu-qe"))
  1056. pdata->flags = SPI_QE_CPU_MODE;
  1057. else if (prop && !strcmp(prop, "qe"))
  1058. pdata->flags = SPI_CPM_MODE | SPI_QE;
  1059. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  1060. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  1061. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  1062. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  1063. ret = of_mpc8xxx_spi_get_chipselects(dev);
  1064. if (ret)
  1065. goto err;
  1066. ret = of_address_to_resource(np, 0, &mem);
  1067. if (ret)
  1068. goto err;
  1069. ret = of_irq_to_resource(np, 0, &irq);
  1070. if (!ret) {
  1071. ret = -EINVAL;
  1072. goto err;
  1073. }
  1074. master = mpc8xxx_spi_probe(dev, &mem, irq.start);
  1075. if (IS_ERR(master)) {
  1076. ret = PTR_ERR(master);
  1077. goto err;
  1078. }
  1079. return 0;
  1080. err:
  1081. of_mpc8xxx_spi_free_chipselects(dev);
  1082. err_clk:
  1083. kfree(pinfo);
  1084. return ret;
  1085. }
  1086. static int __devexit of_mpc8xxx_spi_remove(struct platform_device *ofdev)
  1087. {
  1088. int ret;
  1089. ret = mpc8xxx_spi_remove(&ofdev->dev);
  1090. if (ret)
  1091. return ret;
  1092. of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
  1093. return 0;
  1094. }
  1095. static const struct of_device_id of_mpc8xxx_spi_match[] = {
  1096. { .compatible = "fsl,spi" },
  1097. {},
  1098. };
  1099. MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
  1100. static struct of_platform_driver of_mpc8xxx_spi_driver = {
  1101. .driver = {
  1102. .name = "mpc8xxx_spi",
  1103. .owner = THIS_MODULE,
  1104. .of_match_table = of_mpc8xxx_spi_match,
  1105. },
  1106. .probe = of_mpc8xxx_spi_probe,
  1107. .remove = __devexit_p(of_mpc8xxx_spi_remove),
  1108. };
  1109. #ifdef CONFIG_MPC832x_RDB
  1110. /*
  1111. * XXX XXX XXX
  1112. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  1113. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  1114. * tree can work with OpenFirmware driver. But for now we support old trees
  1115. * as well.
  1116. */
  1117. static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  1118. {
  1119. struct resource *mem;
  1120. int irq;
  1121. struct spi_master *master;
  1122. if (!pdev->dev.platform_data)
  1123. return -EINVAL;
  1124. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1125. if (!mem)
  1126. return -EINVAL;
  1127. irq = platform_get_irq(pdev, 0);
  1128. if (irq <= 0)
  1129. return -EINVAL;
  1130. master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
  1131. if (IS_ERR(master))
  1132. return PTR_ERR(master);
  1133. return 0;
  1134. }
  1135. static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  1136. {
  1137. return mpc8xxx_spi_remove(&pdev->dev);
  1138. }
  1139. MODULE_ALIAS("platform:mpc8xxx_spi");
  1140. static struct platform_driver mpc8xxx_spi_driver = {
  1141. .probe = plat_mpc8xxx_spi_probe,
  1142. .remove = __devexit_p(plat_mpc8xxx_spi_remove),
  1143. .driver = {
  1144. .name = "mpc8xxx_spi",
  1145. .owner = THIS_MODULE,
  1146. },
  1147. };
  1148. static bool legacy_driver_failed;
  1149. static void __init legacy_driver_register(void)
  1150. {
  1151. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  1152. }
  1153. static void __exit legacy_driver_unregister(void)
  1154. {
  1155. if (legacy_driver_failed)
  1156. return;
  1157. platform_driver_unregister(&mpc8xxx_spi_driver);
  1158. }
  1159. #else
  1160. static void __init legacy_driver_register(void) {}
  1161. static void __exit legacy_driver_unregister(void) {}
  1162. #endif /* CONFIG_MPC832x_RDB */
  1163. static int __init mpc8xxx_spi_init(void)
  1164. {
  1165. legacy_driver_register();
  1166. return of_register_platform_driver(&of_mpc8xxx_spi_driver);
  1167. }
  1168. static void __exit mpc8xxx_spi_exit(void)
  1169. {
  1170. of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
  1171. legacy_driver_unregister();
  1172. }
  1173. module_init(mpc8xxx_spi_init);
  1174. module_exit(mpc8xxx_spi_exit);
  1175. MODULE_AUTHOR("Kumar Gala");
  1176. MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
  1177. MODULE_LICENSE("GPL");