spi_mpc8xxx.c 34 KB

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