spi-pxa2xx.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. #define LPSS_PRIV_CLOCK_GATE 0x38
  71. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
  72. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
  73. struct lpss_config {
  74. /* LPSS offset from drv_data->ioaddr */
  75. unsigned offset;
  76. /* Register offsets from drv_data->lpss_base or -1 */
  77. int reg_general;
  78. int reg_ssp;
  79. int reg_cs_ctrl;
  80. int reg_capabilities;
  81. /* FIFO thresholds */
  82. u32 rx_threshold;
  83. u32 tx_threshold_lo;
  84. u32 tx_threshold_hi;
  85. /* Chip select control */
  86. unsigned cs_sel_shift;
  87. unsigned cs_sel_mask;
  88. unsigned cs_num;
  89. /* Quirks */
  90. unsigned cs_clk_stays_gated : 1;
  91. };
  92. /* Keep these sorted with enum pxa_ssp_type */
  93. static const struct lpss_config lpss_platforms[] = {
  94. { /* LPSS_LPT_SSP */
  95. .offset = 0x800,
  96. .reg_general = 0x08,
  97. .reg_ssp = 0x0c,
  98. .reg_cs_ctrl = 0x18,
  99. .reg_capabilities = -1,
  100. .rx_threshold = 64,
  101. .tx_threshold_lo = 160,
  102. .tx_threshold_hi = 224,
  103. },
  104. { /* LPSS_BYT_SSP */
  105. .offset = 0x400,
  106. .reg_general = 0x08,
  107. .reg_ssp = 0x0c,
  108. .reg_cs_ctrl = 0x18,
  109. .reg_capabilities = -1,
  110. .rx_threshold = 64,
  111. .tx_threshold_lo = 160,
  112. .tx_threshold_hi = 224,
  113. },
  114. { /* LPSS_BSW_SSP */
  115. .offset = 0x400,
  116. .reg_general = 0x08,
  117. .reg_ssp = 0x0c,
  118. .reg_cs_ctrl = 0x18,
  119. .reg_capabilities = -1,
  120. .rx_threshold = 64,
  121. .tx_threshold_lo = 160,
  122. .tx_threshold_hi = 224,
  123. .cs_sel_shift = 2,
  124. .cs_sel_mask = 1 << 2,
  125. .cs_num = 2,
  126. },
  127. { /* LPSS_SPT_SSP */
  128. .offset = 0x200,
  129. .reg_general = -1,
  130. .reg_ssp = 0x20,
  131. .reg_cs_ctrl = 0x24,
  132. .reg_capabilities = -1,
  133. .rx_threshold = 1,
  134. .tx_threshold_lo = 32,
  135. .tx_threshold_hi = 56,
  136. },
  137. { /* LPSS_BXT_SSP */
  138. .offset = 0x200,
  139. .reg_general = -1,
  140. .reg_ssp = 0x20,
  141. .reg_cs_ctrl = 0x24,
  142. .reg_capabilities = 0xfc,
  143. .rx_threshold = 1,
  144. .tx_threshold_lo = 16,
  145. .tx_threshold_hi = 48,
  146. .cs_sel_shift = 8,
  147. .cs_sel_mask = 3 << 8,
  148. .cs_clk_stays_gated = true,
  149. },
  150. { /* LPSS_CNL_SSP */
  151. .offset = 0x200,
  152. .reg_general = -1,
  153. .reg_ssp = 0x20,
  154. .reg_cs_ctrl = 0x24,
  155. .reg_capabilities = 0xfc,
  156. .rx_threshold = 1,
  157. .tx_threshold_lo = 32,
  158. .tx_threshold_hi = 56,
  159. .cs_sel_shift = 8,
  160. .cs_sel_mask = 3 << 8,
  161. .cs_clk_stays_gated = true,
  162. },
  163. };
  164. static inline const struct lpss_config
  165. *lpss_get_config(const struct driver_data *drv_data)
  166. {
  167. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  168. }
  169. static bool is_lpss_ssp(const struct driver_data *drv_data)
  170. {
  171. switch (drv_data->ssp_type) {
  172. case LPSS_LPT_SSP:
  173. case LPSS_BYT_SSP:
  174. case LPSS_BSW_SSP:
  175. case LPSS_SPT_SSP:
  176. case LPSS_BXT_SSP:
  177. case LPSS_CNL_SSP:
  178. return true;
  179. default:
  180. return false;
  181. }
  182. }
  183. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  184. {
  185. return drv_data->ssp_type == QUARK_X1000_SSP;
  186. }
  187. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  188. {
  189. switch (drv_data->ssp_type) {
  190. case QUARK_X1000_SSP:
  191. return QUARK_X1000_SSCR1_CHANGE_MASK;
  192. case CE4100_SSP:
  193. return CE4100_SSCR1_CHANGE_MASK;
  194. default:
  195. return SSCR1_CHANGE_MASK;
  196. }
  197. }
  198. static u32
  199. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  200. {
  201. switch (drv_data->ssp_type) {
  202. case QUARK_X1000_SSP:
  203. return RX_THRESH_QUARK_X1000_DFLT;
  204. case CE4100_SSP:
  205. return RX_THRESH_CE4100_DFLT;
  206. default:
  207. return RX_THRESH_DFLT;
  208. }
  209. }
  210. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  211. {
  212. u32 mask;
  213. switch (drv_data->ssp_type) {
  214. case QUARK_X1000_SSP:
  215. mask = QUARK_X1000_SSSR_TFL_MASK;
  216. break;
  217. case CE4100_SSP:
  218. mask = CE4100_SSSR_TFL_MASK;
  219. break;
  220. default:
  221. mask = SSSR_TFL_MASK;
  222. break;
  223. }
  224. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  225. }
  226. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  227. u32 *sccr1_reg)
  228. {
  229. u32 mask;
  230. switch (drv_data->ssp_type) {
  231. case QUARK_X1000_SSP:
  232. mask = QUARK_X1000_SSCR1_RFT;
  233. break;
  234. case CE4100_SSP:
  235. mask = CE4100_SSCR1_RFT;
  236. break;
  237. default:
  238. mask = SSCR1_RFT;
  239. break;
  240. }
  241. *sccr1_reg &= ~mask;
  242. }
  243. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  244. u32 *sccr1_reg, u32 threshold)
  245. {
  246. switch (drv_data->ssp_type) {
  247. case QUARK_X1000_SSP:
  248. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  249. break;
  250. case CE4100_SSP:
  251. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  252. break;
  253. default:
  254. *sccr1_reg |= SSCR1_RxTresh(threshold);
  255. break;
  256. }
  257. }
  258. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  259. u32 clk_div, u8 bits)
  260. {
  261. switch (drv_data->ssp_type) {
  262. case QUARK_X1000_SSP:
  263. return clk_div
  264. | QUARK_X1000_SSCR0_Motorola
  265. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  266. | SSCR0_SSE;
  267. default:
  268. return clk_div
  269. | SSCR0_Motorola
  270. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  271. | SSCR0_SSE
  272. | (bits > 16 ? SSCR0_EDSS : 0);
  273. }
  274. }
  275. /*
  276. * Read and write LPSS SSP private registers. Caller must first check that
  277. * is_lpss_ssp() returns true before these can be called.
  278. */
  279. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  280. {
  281. WARN_ON(!drv_data->lpss_base);
  282. return readl(drv_data->lpss_base + offset);
  283. }
  284. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  285. unsigned offset, u32 value)
  286. {
  287. WARN_ON(!drv_data->lpss_base);
  288. writel(value, drv_data->lpss_base + offset);
  289. }
  290. /*
  291. * lpss_ssp_setup - perform LPSS SSP specific setup
  292. * @drv_data: pointer to the driver private data
  293. *
  294. * Perform LPSS SSP specific setup. This function must be called first if
  295. * one is going to use LPSS SSP private registers.
  296. */
  297. static void lpss_ssp_setup(struct driver_data *drv_data)
  298. {
  299. const struct lpss_config *config;
  300. u32 value;
  301. config = lpss_get_config(drv_data);
  302. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  303. /* Enable software chip select control */
  304. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  305. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  306. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  307. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  308. /* Enable multiblock DMA transfers */
  309. if (drv_data->master_info->enable_dma) {
  310. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  311. if (config->reg_general >= 0) {
  312. value = __lpss_ssp_read_priv(drv_data,
  313. config->reg_general);
  314. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  315. __lpss_ssp_write_priv(drv_data,
  316. config->reg_general, value);
  317. }
  318. }
  319. }
  320. static void lpss_ssp_select_cs(struct spi_device *spi,
  321. const struct lpss_config *config)
  322. {
  323. struct driver_data *drv_data =
  324. spi_controller_get_devdata(spi->controller);
  325. u32 value, cs;
  326. if (!config->cs_sel_mask)
  327. return;
  328. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  329. cs = spi->chip_select;
  330. cs <<= config->cs_sel_shift;
  331. if (cs != (value & config->cs_sel_mask)) {
  332. /*
  333. * When switching another chip select output active the
  334. * output must be selected first and wait 2 ssp_clk cycles
  335. * before changing state to active. Otherwise a short
  336. * glitch will occur on the previous chip select since
  337. * output select is latched but state control is not.
  338. */
  339. value &= ~config->cs_sel_mask;
  340. value |= cs;
  341. __lpss_ssp_write_priv(drv_data,
  342. config->reg_cs_ctrl, value);
  343. ndelay(1000000000 /
  344. (drv_data->master->max_speed_hz / 2));
  345. }
  346. }
  347. static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
  348. {
  349. struct driver_data *drv_data =
  350. spi_controller_get_devdata(spi->controller);
  351. const struct lpss_config *config;
  352. u32 value;
  353. config = lpss_get_config(drv_data);
  354. if (enable)
  355. lpss_ssp_select_cs(spi, config);
  356. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  357. if (enable)
  358. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  359. else
  360. value |= LPSS_CS_CONTROL_CS_HIGH;
  361. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  362. if (config->cs_clk_stays_gated) {
  363. u32 clkgate;
  364. /*
  365. * Changing CS alone when dynamic clock gating is on won't
  366. * actually flip CS at that time. This ruins SPI transfers
  367. * that specify delays, or have no data. Toggle the clock mode
  368. * to force on briefly to poke the CS pin to move.
  369. */
  370. clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
  371. value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
  372. LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
  373. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
  374. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
  375. }
  376. }
  377. static void cs_assert(struct spi_device *spi)
  378. {
  379. struct chip_data *chip = spi_get_ctldata(spi);
  380. struct driver_data *drv_data =
  381. spi_controller_get_devdata(spi->controller);
  382. if (drv_data->ssp_type == CE4100_SSP) {
  383. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  384. return;
  385. }
  386. if (chip->cs_control) {
  387. chip->cs_control(PXA2XX_CS_ASSERT);
  388. return;
  389. }
  390. if (chip->gpiod_cs) {
  391. gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
  392. return;
  393. }
  394. if (is_lpss_ssp(drv_data))
  395. lpss_ssp_cs_control(spi, true);
  396. }
  397. static void cs_deassert(struct spi_device *spi)
  398. {
  399. struct chip_data *chip = spi_get_ctldata(spi);
  400. struct driver_data *drv_data =
  401. spi_controller_get_devdata(spi->controller);
  402. unsigned long timeout;
  403. if (drv_data->ssp_type == CE4100_SSP)
  404. return;
  405. /* Wait until SSP becomes idle before deasserting the CS */
  406. timeout = jiffies + msecs_to_jiffies(10);
  407. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  408. !time_after(jiffies, timeout))
  409. cpu_relax();
  410. if (chip->cs_control) {
  411. chip->cs_control(PXA2XX_CS_DEASSERT);
  412. return;
  413. }
  414. if (chip->gpiod_cs) {
  415. gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
  416. return;
  417. }
  418. if (is_lpss_ssp(drv_data))
  419. lpss_ssp_cs_control(spi, false);
  420. }
  421. static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
  422. {
  423. if (level)
  424. cs_deassert(spi);
  425. else
  426. cs_assert(spi);
  427. }
  428. int pxa2xx_spi_flush(struct driver_data *drv_data)
  429. {
  430. unsigned long limit = loops_per_jiffy << 1;
  431. do {
  432. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  433. pxa2xx_spi_read(drv_data, SSDR);
  434. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  435. write_SSSR_CS(drv_data, SSSR_ROR);
  436. return limit;
  437. }
  438. static int null_writer(struct driver_data *drv_data)
  439. {
  440. u8 n_bytes = drv_data->n_bytes;
  441. if (pxa2xx_spi_txfifo_full(drv_data)
  442. || (drv_data->tx == drv_data->tx_end))
  443. return 0;
  444. pxa2xx_spi_write(drv_data, SSDR, 0);
  445. drv_data->tx += n_bytes;
  446. return 1;
  447. }
  448. static int null_reader(struct driver_data *drv_data)
  449. {
  450. u8 n_bytes = drv_data->n_bytes;
  451. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  452. && (drv_data->rx < drv_data->rx_end)) {
  453. pxa2xx_spi_read(drv_data, SSDR);
  454. drv_data->rx += n_bytes;
  455. }
  456. return drv_data->rx == drv_data->rx_end;
  457. }
  458. static int u8_writer(struct driver_data *drv_data)
  459. {
  460. if (pxa2xx_spi_txfifo_full(drv_data)
  461. || (drv_data->tx == drv_data->tx_end))
  462. return 0;
  463. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  464. ++drv_data->tx;
  465. return 1;
  466. }
  467. static int u8_reader(struct driver_data *drv_data)
  468. {
  469. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  470. && (drv_data->rx < drv_data->rx_end)) {
  471. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  472. ++drv_data->rx;
  473. }
  474. return drv_data->rx == drv_data->rx_end;
  475. }
  476. static int u16_writer(struct driver_data *drv_data)
  477. {
  478. if (pxa2xx_spi_txfifo_full(drv_data)
  479. || (drv_data->tx == drv_data->tx_end))
  480. return 0;
  481. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  482. drv_data->tx += 2;
  483. return 1;
  484. }
  485. static int u16_reader(struct driver_data *drv_data)
  486. {
  487. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  488. && (drv_data->rx < drv_data->rx_end)) {
  489. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  490. drv_data->rx += 2;
  491. }
  492. return drv_data->rx == drv_data->rx_end;
  493. }
  494. static int u32_writer(struct driver_data *drv_data)
  495. {
  496. if (pxa2xx_spi_txfifo_full(drv_data)
  497. || (drv_data->tx == drv_data->tx_end))
  498. return 0;
  499. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  500. drv_data->tx += 4;
  501. return 1;
  502. }
  503. static int u32_reader(struct driver_data *drv_data)
  504. {
  505. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  506. && (drv_data->rx < drv_data->rx_end)) {
  507. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  508. drv_data->rx += 4;
  509. }
  510. return drv_data->rx == drv_data->rx_end;
  511. }
  512. static void reset_sccr1(struct driver_data *drv_data)
  513. {
  514. struct chip_data *chip =
  515. spi_get_ctldata(drv_data->master->cur_msg->spi);
  516. u32 sccr1_reg;
  517. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  518. switch (drv_data->ssp_type) {
  519. case QUARK_X1000_SSP:
  520. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  521. break;
  522. case CE4100_SSP:
  523. sccr1_reg &= ~CE4100_SSCR1_RFT;
  524. break;
  525. default:
  526. sccr1_reg &= ~SSCR1_RFT;
  527. break;
  528. }
  529. sccr1_reg |= chip->threshold;
  530. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  531. }
  532. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  533. {
  534. /* Stop and reset SSP */
  535. write_SSSR_CS(drv_data, drv_data->clear_sr);
  536. reset_sccr1(drv_data);
  537. if (!pxa25x_ssp_comp(drv_data))
  538. pxa2xx_spi_write(drv_data, SSTO, 0);
  539. pxa2xx_spi_flush(drv_data);
  540. pxa2xx_spi_write(drv_data, SSCR0,
  541. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  542. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  543. drv_data->master->cur_msg->status = -EIO;
  544. spi_finalize_current_transfer(drv_data->master);
  545. }
  546. static void int_transfer_complete(struct driver_data *drv_data)
  547. {
  548. /* Clear and disable interrupts */
  549. write_SSSR_CS(drv_data, drv_data->clear_sr);
  550. reset_sccr1(drv_data);
  551. if (!pxa25x_ssp_comp(drv_data))
  552. pxa2xx_spi_write(drv_data, SSTO, 0);
  553. spi_finalize_current_transfer(drv_data->master);
  554. }
  555. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  556. {
  557. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  558. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  559. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  560. if (irq_status & SSSR_ROR) {
  561. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  562. return IRQ_HANDLED;
  563. }
  564. if (irq_status & SSSR_TINT) {
  565. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  566. if (drv_data->read(drv_data)) {
  567. int_transfer_complete(drv_data);
  568. return IRQ_HANDLED;
  569. }
  570. }
  571. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  572. do {
  573. if (drv_data->read(drv_data)) {
  574. int_transfer_complete(drv_data);
  575. return IRQ_HANDLED;
  576. }
  577. } while (drv_data->write(drv_data));
  578. if (drv_data->read(drv_data)) {
  579. int_transfer_complete(drv_data);
  580. return IRQ_HANDLED;
  581. }
  582. if (drv_data->tx == drv_data->tx_end) {
  583. u32 bytes_left;
  584. u32 sccr1_reg;
  585. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  586. sccr1_reg &= ~SSCR1_TIE;
  587. /*
  588. * PXA25x_SSP has no timeout, set up rx threshould for the
  589. * remaining RX bytes.
  590. */
  591. if (pxa25x_ssp_comp(drv_data)) {
  592. u32 rx_thre;
  593. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  594. bytes_left = drv_data->rx_end - drv_data->rx;
  595. switch (drv_data->n_bytes) {
  596. case 4:
  597. bytes_left >>= 1;
  598. case 2:
  599. bytes_left >>= 1;
  600. }
  601. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  602. if (rx_thre > bytes_left)
  603. rx_thre = bytes_left;
  604. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  605. }
  606. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  607. }
  608. /* We did something */
  609. return IRQ_HANDLED;
  610. }
  611. static void handle_bad_msg(struct driver_data *drv_data)
  612. {
  613. pxa2xx_spi_write(drv_data, SSCR0,
  614. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  615. pxa2xx_spi_write(drv_data, SSCR1,
  616. pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
  617. if (!pxa25x_ssp_comp(drv_data))
  618. pxa2xx_spi_write(drv_data, SSTO, 0);
  619. write_SSSR_CS(drv_data, drv_data->clear_sr);
  620. dev_err(&drv_data->pdev->dev,
  621. "bad message state in interrupt handler\n");
  622. }
  623. static irqreturn_t ssp_int(int irq, void *dev_id)
  624. {
  625. struct driver_data *drv_data = dev_id;
  626. u32 sccr1_reg;
  627. u32 mask = drv_data->mask_sr;
  628. u32 status;
  629. /*
  630. * The IRQ might be shared with other peripherals so we must first
  631. * check that are we RPM suspended or not. If we are we assume that
  632. * the IRQ was not for us (we shouldn't be RPM suspended when the
  633. * interrupt is enabled).
  634. */
  635. if (pm_runtime_suspended(&drv_data->pdev->dev))
  636. return IRQ_NONE;
  637. /*
  638. * If the device is not yet in RPM suspended state and we get an
  639. * interrupt that is meant for another device, check if status bits
  640. * are all set to one. That means that the device is already
  641. * powered off.
  642. */
  643. status = pxa2xx_spi_read(drv_data, SSSR);
  644. if (status == ~0)
  645. return IRQ_NONE;
  646. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  647. /* Ignore possible writes if we don't need to write */
  648. if (!(sccr1_reg & SSCR1_TIE))
  649. mask &= ~SSSR_TFS;
  650. /* Ignore RX timeout interrupt if it is disabled */
  651. if (!(sccr1_reg & SSCR1_TINTE))
  652. mask &= ~SSSR_TINT;
  653. if (!(status & mask))
  654. return IRQ_NONE;
  655. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  656. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  657. if (!drv_data->master->cur_msg) {
  658. handle_bad_msg(drv_data);
  659. /* Never fail */
  660. return IRQ_HANDLED;
  661. }
  662. return drv_data->transfer_handler(drv_data);
  663. }
  664. /*
  665. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  666. * input frequency by fractions of 2^24. It also has a divider by 5.
  667. *
  668. * There are formulas to get baud rate value for given input frequency and
  669. * divider parameters, such as DDS_CLK_RATE and SCR:
  670. *
  671. * Fsys = 200MHz
  672. *
  673. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  674. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  675. *
  676. * DDS_CLK_RATE either 2^n or 2^n / 5.
  677. * SCR is in range 0 .. 255
  678. *
  679. * Divisor = 5^i * 2^j * 2 * k
  680. * i = [0, 1] i = 1 iff j = 0 or j > 3
  681. * j = [0, 23] j = 0 iff i = 1
  682. * k = [1, 256]
  683. * Special case: j = 0, i = 1: Divisor = 2 / 5
  684. *
  685. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  686. * are:
  687. * Case 1: 2^n, n = [0, 23]
  688. * Case 2: 2^24 * 2 / 5 (0x666666)
  689. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  690. *
  691. * In all cases the lowest possible value is better.
  692. *
  693. * The function calculates parameters for all cases and chooses the one closest
  694. * to the asked baud rate.
  695. */
  696. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  697. {
  698. unsigned long xtal = 200000000;
  699. unsigned long fref = xtal / 2; /* mandatory division by 2,
  700. see (2) */
  701. /* case 3 */
  702. unsigned long fref1 = fref / 2; /* case 1 */
  703. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  704. unsigned long scale;
  705. unsigned long q, q1, q2;
  706. long r, r1, r2;
  707. u32 mul;
  708. /* Case 1 */
  709. /* Set initial value for DDS_CLK_RATE */
  710. mul = (1 << 24) >> 1;
  711. /* Calculate initial quot */
  712. q1 = DIV_ROUND_UP(fref1, rate);
  713. /* Scale q1 if it's too big */
  714. if (q1 > 256) {
  715. /* Scale q1 to range [1, 512] */
  716. scale = fls_long(q1 - 1);
  717. if (scale > 9) {
  718. q1 >>= scale - 9;
  719. mul >>= scale - 9;
  720. }
  721. /* Round the result if we have a remainder */
  722. q1 += q1 & 1;
  723. }
  724. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  725. scale = __ffs(q1);
  726. q1 >>= scale;
  727. mul >>= scale;
  728. /* Get the remainder */
  729. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  730. /* Case 2 */
  731. q2 = DIV_ROUND_UP(fref2, rate);
  732. r2 = abs(fref2 / q2 - rate);
  733. /*
  734. * Choose the best between two: less remainder we have the better. We
  735. * can't go case 2 if q2 is greater than 256 since SCR register can
  736. * hold only values 0 .. 255.
  737. */
  738. if (r2 >= r1 || q2 > 256) {
  739. /* case 1 is better */
  740. r = r1;
  741. q = q1;
  742. } else {
  743. /* case 2 is better */
  744. r = r2;
  745. q = q2;
  746. mul = (1 << 24) * 2 / 5;
  747. }
  748. /* Check case 3 only if the divisor is big enough */
  749. if (fref / rate >= 80) {
  750. u64 fssp;
  751. u32 m;
  752. /* Calculate initial quot */
  753. q1 = DIV_ROUND_UP(fref, rate);
  754. m = (1 << 24) / q1;
  755. /* Get the remainder */
  756. fssp = (u64)fref * m;
  757. do_div(fssp, 1 << 24);
  758. r1 = abs(fssp - rate);
  759. /* Choose this one if it suits better */
  760. if (r1 < r) {
  761. /* case 3 is better */
  762. q = 1;
  763. mul = m;
  764. }
  765. }
  766. *dds = mul;
  767. return q - 1;
  768. }
  769. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  770. {
  771. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  772. const struct ssp_device *ssp = drv_data->ssp;
  773. rate = min_t(int, ssp_clk, rate);
  774. /*
  775. * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
  776. * that the SSP transmission rate can be greater than the device rate
  777. */
  778. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  779. return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
  780. else
  781. return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
  782. }
  783. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  784. int rate)
  785. {
  786. struct chip_data *chip =
  787. spi_get_ctldata(drv_data->master->cur_msg->spi);
  788. unsigned int clk_div;
  789. switch (drv_data->ssp_type) {
  790. case QUARK_X1000_SSP:
  791. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  792. break;
  793. default:
  794. clk_div = ssp_get_clk_div(drv_data, rate);
  795. break;
  796. }
  797. return clk_div << 8;
  798. }
  799. static bool pxa2xx_spi_can_dma(struct spi_controller *master,
  800. struct spi_device *spi,
  801. struct spi_transfer *xfer)
  802. {
  803. struct chip_data *chip = spi_get_ctldata(spi);
  804. return chip->enable_dma &&
  805. xfer->len <= MAX_DMA_LEN &&
  806. xfer->len >= chip->dma_burst_size;
  807. }
  808. static int pxa2xx_spi_transfer_one(struct spi_controller *master,
  809. struct spi_device *spi,
  810. struct spi_transfer *transfer)
  811. {
  812. struct driver_data *drv_data = spi_controller_get_devdata(master);
  813. struct spi_message *message = master->cur_msg;
  814. struct chip_data *chip = spi_get_ctldata(message->spi);
  815. u32 dma_thresh = chip->dma_threshold;
  816. u32 dma_burst = chip->dma_burst_size;
  817. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  818. u32 clk_div;
  819. u8 bits;
  820. u32 speed;
  821. u32 cr0;
  822. u32 cr1;
  823. int err;
  824. int dma_mapped;
  825. /* Check if we can DMA this transfer */
  826. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  827. /* reject already-mapped transfers; PIO won't always work */
  828. if (message->is_dma_mapped
  829. || transfer->rx_dma || transfer->tx_dma) {
  830. dev_err(&drv_data->pdev->dev,
  831. "Mapped transfer length of %u is greater than %d\n",
  832. transfer->len, MAX_DMA_LEN);
  833. return -EINVAL;
  834. }
  835. /* warn ... we force this to PIO mode */
  836. dev_warn_ratelimited(&message->spi->dev,
  837. "DMA disabled for transfer length %ld greater than %d\n",
  838. (long)transfer->len, MAX_DMA_LEN);
  839. }
  840. /* Setup the transfer state based on the type of transfer */
  841. if (pxa2xx_spi_flush(drv_data) == 0) {
  842. dev_err(&drv_data->pdev->dev, "Flush failed\n");
  843. return -EIO;
  844. }
  845. drv_data->n_bytes = chip->n_bytes;
  846. drv_data->tx = (void *)transfer->tx_buf;
  847. drv_data->tx_end = drv_data->tx + transfer->len;
  848. drv_data->rx = transfer->rx_buf;
  849. drv_data->rx_end = drv_data->rx + transfer->len;
  850. drv_data->write = drv_data->tx ? chip->write : null_writer;
  851. drv_data->read = drv_data->rx ? chip->read : null_reader;
  852. /* Change speed and bit per word on a per transfer */
  853. bits = transfer->bits_per_word;
  854. speed = transfer->speed_hz;
  855. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  856. if (bits <= 8) {
  857. drv_data->n_bytes = 1;
  858. drv_data->read = drv_data->read != null_reader ?
  859. u8_reader : null_reader;
  860. drv_data->write = drv_data->write != null_writer ?
  861. u8_writer : null_writer;
  862. } else if (bits <= 16) {
  863. drv_data->n_bytes = 2;
  864. drv_data->read = drv_data->read != null_reader ?
  865. u16_reader : null_reader;
  866. drv_data->write = drv_data->write != null_writer ?
  867. u16_writer : null_writer;
  868. } else if (bits <= 32) {
  869. drv_data->n_bytes = 4;
  870. drv_data->read = drv_data->read != null_reader ?
  871. u32_reader : null_reader;
  872. drv_data->write = drv_data->write != null_writer ?
  873. u32_writer : null_writer;
  874. }
  875. /*
  876. * if bits/word is changed in dma mode, then must check the
  877. * thresholds and burst also
  878. */
  879. if (chip->enable_dma) {
  880. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  881. message->spi,
  882. bits, &dma_burst,
  883. &dma_thresh))
  884. dev_warn_ratelimited(&message->spi->dev,
  885. "DMA burst size reduced to match bits_per_word\n");
  886. }
  887. dma_mapped = master->can_dma &&
  888. master->can_dma(master, message->spi, transfer) &&
  889. master->cur_msg_mapped;
  890. if (dma_mapped) {
  891. /* Ensure we have the correct interrupt handler */
  892. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  893. err = pxa2xx_spi_dma_prepare(drv_data, transfer);
  894. if (err)
  895. return err;
  896. /* Clear status and start DMA engine */
  897. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  898. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  899. pxa2xx_spi_dma_start(drv_data);
  900. } else {
  901. /* Ensure we have the correct interrupt handler */
  902. drv_data->transfer_handler = interrupt_transfer;
  903. /* Clear status */
  904. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  905. write_SSSR_CS(drv_data, drv_data->clear_sr);
  906. }
  907. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  908. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  909. if (!pxa25x_ssp_comp(drv_data))
  910. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  911. master->max_speed_hz
  912. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  913. dma_mapped ? "DMA" : "PIO");
  914. else
  915. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  916. master->max_speed_hz / 2
  917. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  918. dma_mapped ? "DMA" : "PIO");
  919. if (is_lpss_ssp(drv_data)) {
  920. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  921. != chip->lpss_rx_threshold)
  922. pxa2xx_spi_write(drv_data, SSIRF,
  923. chip->lpss_rx_threshold);
  924. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  925. != chip->lpss_tx_threshold)
  926. pxa2xx_spi_write(drv_data, SSITF,
  927. chip->lpss_tx_threshold);
  928. }
  929. if (is_quark_x1000_ssp(drv_data) &&
  930. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  931. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  932. /* see if we need to reload the config registers */
  933. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  934. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  935. != (cr1 & change_mask)) {
  936. /* stop the SSP, and update the other bits */
  937. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  938. if (!pxa25x_ssp_comp(drv_data))
  939. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  940. /* first set CR1 without interrupt and service enables */
  941. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  942. /* restart the SSP */
  943. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  944. } else {
  945. if (!pxa25x_ssp_comp(drv_data))
  946. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  947. }
  948. /*
  949. * Release the data by enabling service requests and interrupts,
  950. * without changing any mode bits
  951. */
  952. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  953. return 1;
  954. }
  955. static void pxa2xx_spi_handle_err(struct spi_controller *master,
  956. struct spi_message *msg)
  957. {
  958. struct driver_data *drv_data = spi_controller_get_devdata(master);
  959. /* Disable the SSP */
  960. pxa2xx_spi_write(drv_data, SSCR0,
  961. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  962. /* Clear and disable interrupts and service requests */
  963. write_SSSR_CS(drv_data, drv_data->clear_sr);
  964. pxa2xx_spi_write(drv_data, SSCR1,
  965. pxa2xx_spi_read(drv_data, SSCR1)
  966. & ~(drv_data->int_cr1 | drv_data->dma_cr1));
  967. if (!pxa25x_ssp_comp(drv_data))
  968. pxa2xx_spi_write(drv_data, SSTO, 0);
  969. /*
  970. * Stop the DMA if running. Note DMA callback handler may have unset
  971. * the dma_running already, which is fine as stopping is not needed
  972. * then but we shouldn't rely this flag for anything else than
  973. * stopping. For instance to differentiate between PIO and DMA
  974. * transfers.
  975. */
  976. if (atomic_read(&drv_data->dma_running))
  977. pxa2xx_spi_dma_stop(drv_data);
  978. }
  979. static int pxa2xx_spi_unprepare_transfer(struct spi_controller *master)
  980. {
  981. struct driver_data *drv_data = spi_controller_get_devdata(master);
  982. /* Disable the SSP now */
  983. pxa2xx_spi_write(drv_data, SSCR0,
  984. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  985. return 0;
  986. }
  987. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  988. struct pxa2xx_spi_chip *chip_info)
  989. {
  990. struct driver_data *drv_data =
  991. spi_controller_get_devdata(spi->controller);
  992. struct gpio_desc *gpiod;
  993. int err = 0;
  994. if (chip == NULL)
  995. return 0;
  996. if (drv_data->cs_gpiods) {
  997. gpiod = drv_data->cs_gpiods[spi->chip_select];
  998. if (gpiod) {
  999. chip->gpiod_cs = gpiod;
  1000. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1001. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1002. }
  1003. return 0;
  1004. }
  1005. if (chip_info == NULL)
  1006. return 0;
  1007. /* NOTE: setup() can be called multiple times, possibly with
  1008. * different chip_info, release previously requested GPIO
  1009. */
  1010. if (chip->gpiod_cs) {
  1011. gpiod_put(chip->gpiod_cs);
  1012. chip->gpiod_cs = NULL;
  1013. }
  1014. /* If (*cs_control) is provided, ignore GPIO chip select */
  1015. if (chip_info->cs_control) {
  1016. chip->cs_control = chip_info->cs_control;
  1017. return 0;
  1018. }
  1019. if (gpio_is_valid(chip_info->gpio_cs)) {
  1020. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1021. if (err) {
  1022. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1023. chip_info->gpio_cs);
  1024. return err;
  1025. }
  1026. gpiod = gpio_to_desc(chip_info->gpio_cs);
  1027. chip->gpiod_cs = gpiod;
  1028. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1029. err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
  1030. }
  1031. return err;
  1032. }
  1033. static int setup(struct spi_device *spi)
  1034. {
  1035. struct pxa2xx_spi_chip *chip_info;
  1036. struct chip_data *chip;
  1037. const struct lpss_config *config;
  1038. struct driver_data *drv_data =
  1039. spi_controller_get_devdata(spi->controller);
  1040. uint tx_thres, tx_hi_thres, rx_thres;
  1041. switch (drv_data->ssp_type) {
  1042. case QUARK_X1000_SSP:
  1043. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1044. tx_hi_thres = 0;
  1045. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1046. break;
  1047. case CE4100_SSP:
  1048. tx_thres = TX_THRESH_CE4100_DFLT;
  1049. tx_hi_thres = 0;
  1050. rx_thres = RX_THRESH_CE4100_DFLT;
  1051. break;
  1052. case LPSS_LPT_SSP:
  1053. case LPSS_BYT_SSP:
  1054. case LPSS_BSW_SSP:
  1055. case LPSS_SPT_SSP:
  1056. case LPSS_BXT_SSP:
  1057. case LPSS_CNL_SSP:
  1058. config = lpss_get_config(drv_data);
  1059. tx_thres = config->tx_threshold_lo;
  1060. tx_hi_thres = config->tx_threshold_hi;
  1061. rx_thres = config->rx_threshold;
  1062. break;
  1063. default:
  1064. tx_thres = TX_THRESH_DFLT;
  1065. tx_hi_thres = 0;
  1066. rx_thres = RX_THRESH_DFLT;
  1067. break;
  1068. }
  1069. /* Only alloc on first setup */
  1070. chip = spi_get_ctldata(spi);
  1071. if (!chip) {
  1072. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1073. if (!chip)
  1074. return -ENOMEM;
  1075. if (drv_data->ssp_type == CE4100_SSP) {
  1076. if (spi->chip_select > 4) {
  1077. dev_err(&spi->dev,
  1078. "failed setup: cs number must not be > 4.\n");
  1079. kfree(chip);
  1080. return -EINVAL;
  1081. }
  1082. chip->frm = spi->chip_select;
  1083. }
  1084. chip->enable_dma = drv_data->master_info->enable_dma;
  1085. chip->timeout = TIMOUT_DFLT;
  1086. }
  1087. /* protocol drivers may change the chip settings, so...
  1088. * if chip_info exists, use it */
  1089. chip_info = spi->controller_data;
  1090. /* chip_info isn't always needed */
  1091. chip->cr1 = 0;
  1092. if (chip_info) {
  1093. if (chip_info->timeout)
  1094. chip->timeout = chip_info->timeout;
  1095. if (chip_info->tx_threshold)
  1096. tx_thres = chip_info->tx_threshold;
  1097. if (chip_info->tx_hi_threshold)
  1098. tx_hi_thres = chip_info->tx_hi_threshold;
  1099. if (chip_info->rx_threshold)
  1100. rx_thres = chip_info->rx_threshold;
  1101. chip->dma_threshold = 0;
  1102. if (chip_info->enable_loopback)
  1103. chip->cr1 = SSCR1_LBM;
  1104. }
  1105. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1106. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1107. | SSITF_TxHiThresh(tx_hi_thres);
  1108. /* set dma burst and threshold outside of chip_info path so that if
  1109. * chip_info goes away after setting chip->enable_dma, the
  1110. * burst and threshold can still respond to changes in bits_per_word */
  1111. if (chip->enable_dma) {
  1112. /* set up legal burst and threshold for dma */
  1113. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1114. spi->bits_per_word,
  1115. &chip->dma_burst_size,
  1116. &chip->dma_threshold)) {
  1117. dev_warn(&spi->dev,
  1118. "in setup: DMA burst size reduced to match bits_per_word\n");
  1119. }
  1120. }
  1121. switch (drv_data->ssp_type) {
  1122. case QUARK_X1000_SSP:
  1123. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1124. & QUARK_X1000_SSCR1_RFT)
  1125. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1126. & QUARK_X1000_SSCR1_TFT);
  1127. break;
  1128. case CE4100_SSP:
  1129. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1130. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1131. break;
  1132. default:
  1133. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1134. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1135. break;
  1136. }
  1137. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1138. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1139. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1140. if (spi->mode & SPI_LOOP)
  1141. chip->cr1 |= SSCR1_LBM;
  1142. if (spi->bits_per_word <= 8) {
  1143. chip->n_bytes = 1;
  1144. chip->read = u8_reader;
  1145. chip->write = u8_writer;
  1146. } else if (spi->bits_per_word <= 16) {
  1147. chip->n_bytes = 2;
  1148. chip->read = u16_reader;
  1149. chip->write = u16_writer;
  1150. } else if (spi->bits_per_word <= 32) {
  1151. chip->n_bytes = 4;
  1152. chip->read = u32_reader;
  1153. chip->write = u32_writer;
  1154. }
  1155. spi_set_ctldata(spi, chip);
  1156. if (drv_data->ssp_type == CE4100_SSP)
  1157. return 0;
  1158. return setup_cs(spi, chip, chip_info);
  1159. }
  1160. static void cleanup(struct spi_device *spi)
  1161. {
  1162. struct chip_data *chip = spi_get_ctldata(spi);
  1163. struct driver_data *drv_data =
  1164. spi_controller_get_devdata(spi->controller);
  1165. if (!chip)
  1166. return;
  1167. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1168. chip->gpiod_cs)
  1169. gpiod_put(chip->gpiod_cs);
  1170. kfree(chip);
  1171. }
  1172. #ifdef CONFIG_PCI
  1173. #ifdef CONFIG_ACPI
  1174. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1175. { "INT33C0", LPSS_LPT_SSP },
  1176. { "INT33C1", LPSS_LPT_SSP },
  1177. { "INT3430", LPSS_LPT_SSP },
  1178. { "INT3431", LPSS_LPT_SSP },
  1179. { "80860F0E", LPSS_BYT_SSP },
  1180. { "8086228E", LPSS_BSW_SSP },
  1181. { },
  1182. };
  1183. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1184. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1185. {
  1186. unsigned int devid;
  1187. int port_id = -1;
  1188. if (adev && adev->pnp.unique_id &&
  1189. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1190. port_id = devid;
  1191. return port_id;
  1192. }
  1193. #else /* !CONFIG_ACPI */
  1194. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1195. {
  1196. return -1;
  1197. }
  1198. #endif
  1199. /*
  1200. * PCI IDs of compound devices that integrate both host controller and private
  1201. * integrated DMA engine. Please note these are not used in module
  1202. * autoloading and probing in this module but matching the LPSS SSP type.
  1203. */
  1204. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1205. /* SPT-LP */
  1206. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1207. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1208. /* SPT-H */
  1209. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1210. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1211. /* KBL-H */
  1212. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1213. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1214. /* BXT A-Step */
  1215. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1216. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1217. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1218. /* BXT B-Step */
  1219. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1220. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1221. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1222. /* GLK */
  1223. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1224. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1225. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1226. /* ICL-LP */
  1227. { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
  1228. { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
  1229. { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
  1230. /* APL */
  1231. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1232. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1233. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1234. /* CNL-LP */
  1235. { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
  1236. { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
  1237. { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
  1238. /* CNL-H */
  1239. { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
  1240. { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
  1241. { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
  1242. { },
  1243. };
  1244. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1245. {
  1246. return param == chan->device->dev;
  1247. }
  1248. static struct pxa2xx_spi_master *
  1249. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1250. {
  1251. struct pxa2xx_spi_master *pdata;
  1252. struct acpi_device *adev;
  1253. struct ssp_device *ssp;
  1254. struct resource *res;
  1255. const struct acpi_device_id *adev_id = NULL;
  1256. const struct pci_device_id *pcidev_id = NULL;
  1257. int type;
  1258. adev = ACPI_COMPANION(&pdev->dev);
  1259. if (dev_is_pci(pdev->dev.parent))
  1260. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1261. to_pci_dev(pdev->dev.parent));
  1262. else if (adev)
  1263. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1264. &pdev->dev);
  1265. else
  1266. return NULL;
  1267. if (adev_id)
  1268. type = (int)adev_id->driver_data;
  1269. else if (pcidev_id)
  1270. type = (int)pcidev_id->driver_data;
  1271. else
  1272. return NULL;
  1273. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1274. if (!pdata)
  1275. return NULL;
  1276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1277. if (!res)
  1278. return NULL;
  1279. ssp = &pdata->ssp;
  1280. ssp->phys_base = res->start;
  1281. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1282. if (IS_ERR(ssp->mmio_base))
  1283. return NULL;
  1284. if (pcidev_id) {
  1285. pdata->tx_param = pdev->dev.parent;
  1286. pdata->rx_param = pdev->dev.parent;
  1287. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1288. }
  1289. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1290. if (IS_ERR(ssp->clk))
  1291. return NULL;
  1292. ssp->irq = platform_get_irq(pdev, 0);
  1293. if (ssp->irq < 0)
  1294. return NULL;
  1295. ssp->type = type;
  1296. ssp->pdev = pdev;
  1297. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1298. pdata->num_chipselect = 1;
  1299. pdata->enable_dma = true;
  1300. return pdata;
  1301. }
  1302. #else /* !CONFIG_PCI */
  1303. static inline struct pxa2xx_spi_master *
  1304. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1305. {
  1306. return NULL;
  1307. }
  1308. #endif
  1309. static int pxa2xx_spi_fw_translate_cs(struct spi_controller *master,
  1310. unsigned int cs)
  1311. {
  1312. struct driver_data *drv_data = spi_controller_get_devdata(master);
  1313. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1314. switch (drv_data->ssp_type) {
  1315. /*
  1316. * For Atoms the ACPI DeviceSelection used by the Windows
  1317. * driver starts from 1 instead of 0 so translate it here
  1318. * to match what Linux expects.
  1319. */
  1320. case LPSS_BYT_SSP:
  1321. case LPSS_BSW_SSP:
  1322. return cs - 1;
  1323. default:
  1324. break;
  1325. }
  1326. }
  1327. return cs;
  1328. }
  1329. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1330. {
  1331. struct device *dev = &pdev->dev;
  1332. struct pxa2xx_spi_master *platform_info;
  1333. struct spi_controller *master;
  1334. struct driver_data *drv_data;
  1335. struct ssp_device *ssp;
  1336. const struct lpss_config *config;
  1337. int status, count;
  1338. u32 tmp;
  1339. platform_info = dev_get_platdata(dev);
  1340. if (!platform_info) {
  1341. platform_info = pxa2xx_spi_init_pdata(pdev);
  1342. if (!platform_info) {
  1343. dev_err(&pdev->dev, "missing platform data\n");
  1344. return -ENODEV;
  1345. }
  1346. }
  1347. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1348. if (!ssp)
  1349. ssp = &platform_info->ssp;
  1350. if (!ssp->mmio_base) {
  1351. dev_err(&pdev->dev, "failed to get ssp\n");
  1352. return -ENODEV;
  1353. }
  1354. master = devm_spi_alloc_master(dev, sizeof(*drv_data));
  1355. if (!master) {
  1356. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1357. pxa_ssp_free(ssp);
  1358. return -ENOMEM;
  1359. }
  1360. drv_data = spi_controller_get_devdata(master);
  1361. drv_data->master = master;
  1362. drv_data->master_info = platform_info;
  1363. drv_data->pdev = pdev;
  1364. drv_data->ssp = ssp;
  1365. master->dev.of_node = pdev->dev.of_node;
  1366. /* the spi->mode bits understood by this driver: */
  1367. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1368. master->bus_num = ssp->port_id;
  1369. master->dma_alignment = DMA_ALIGNMENT;
  1370. master->cleanup = cleanup;
  1371. master->setup = setup;
  1372. master->set_cs = pxa2xx_spi_set_cs;
  1373. master->transfer_one = pxa2xx_spi_transfer_one;
  1374. master->handle_err = pxa2xx_spi_handle_err;
  1375. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1376. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1377. master->auto_runtime_pm = true;
  1378. master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
  1379. drv_data->ssp_type = ssp->type;
  1380. drv_data->ioaddr = ssp->mmio_base;
  1381. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1382. if (pxa25x_ssp_comp(drv_data)) {
  1383. switch (drv_data->ssp_type) {
  1384. case QUARK_X1000_SSP:
  1385. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1386. break;
  1387. default:
  1388. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1389. break;
  1390. }
  1391. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1392. drv_data->dma_cr1 = 0;
  1393. drv_data->clear_sr = SSSR_ROR;
  1394. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1395. } else {
  1396. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1397. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1398. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1399. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1400. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1401. }
  1402. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1403. drv_data);
  1404. if (status < 0) {
  1405. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1406. goto out_error_master_alloc;
  1407. }
  1408. /* Setup DMA if requested */
  1409. if (platform_info->enable_dma) {
  1410. status = pxa2xx_spi_dma_setup(drv_data);
  1411. if (status) {
  1412. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1413. platform_info->enable_dma = false;
  1414. } else {
  1415. master->can_dma = pxa2xx_spi_can_dma;
  1416. master->max_dma_len = MAX_DMA_LEN;
  1417. }
  1418. }
  1419. /* Enable SOC clock */
  1420. status = clk_prepare_enable(ssp->clk);
  1421. if (status)
  1422. goto out_error_dma_irq_alloc;
  1423. master->max_speed_hz = clk_get_rate(ssp->clk);
  1424. /* Load default SSP configuration */
  1425. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1426. switch (drv_data->ssp_type) {
  1427. case QUARK_X1000_SSP:
  1428. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1429. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1430. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1431. /* using the Motorola SPI protocol and use 8 bit frame */
  1432. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1433. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1434. break;
  1435. case CE4100_SSP:
  1436. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1437. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1438. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1439. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1440. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1441. break;
  1442. default:
  1443. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1444. SSCR1_TxTresh(TX_THRESH_DFLT);
  1445. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1446. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1447. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1448. break;
  1449. }
  1450. if (!pxa25x_ssp_comp(drv_data))
  1451. pxa2xx_spi_write(drv_data, SSTO, 0);
  1452. if (!is_quark_x1000_ssp(drv_data))
  1453. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1454. if (is_lpss_ssp(drv_data)) {
  1455. lpss_ssp_setup(drv_data);
  1456. config = lpss_get_config(drv_data);
  1457. if (config->reg_capabilities >= 0) {
  1458. tmp = __lpss_ssp_read_priv(drv_data,
  1459. config->reg_capabilities);
  1460. tmp &= LPSS_CAPS_CS_EN_MASK;
  1461. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1462. platform_info->num_chipselect = ffz(tmp);
  1463. } else if (config->cs_num) {
  1464. platform_info->num_chipselect = config->cs_num;
  1465. }
  1466. }
  1467. master->num_chipselect = platform_info->num_chipselect;
  1468. count = gpiod_count(&pdev->dev, "cs");
  1469. if (count > 0) {
  1470. int i;
  1471. master->num_chipselect = max_t(int, count,
  1472. master->num_chipselect);
  1473. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1474. master->num_chipselect, sizeof(struct gpio_desc *),
  1475. GFP_KERNEL);
  1476. if (!drv_data->cs_gpiods) {
  1477. status = -ENOMEM;
  1478. goto out_error_clock_enabled;
  1479. }
  1480. for (i = 0; i < master->num_chipselect; i++) {
  1481. struct gpio_desc *gpiod;
  1482. gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
  1483. if (IS_ERR(gpiod)) {
  1484. /* Means use native chip select */
  1485. if (PTR_ERR(gpiod) == -ENOENT)
  1486. continue;
  1487. status = (int)PTR_ERR(gpiod);
  1488. goto out_error_clock_enabled;
  1489. } else {
  1490. drv_data->cs_gpiods[i] = gpiod;
  1491. }
  1492. }
  1493. }
  1494. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1495. pm_runtime_use_autosuspend(&pdev->dev);
  1496. pm_runtime_set_active(&pdev->dev);
  1497. pm_runtime_enable(&pdev->dev);
  1498. /* Register with the SPI framework */
  1499. platform_set_drvdata(pdev, drv_data);
  1500. status = spi_register_controller(master);
  1501. if (status != 0) {
  1502. dev_err(&pdev->dev, "problem registering spi master\n");
  1503. goto out_error_pm_runtime_enabled;
  1504. }
  1505. return status;
  1506. out_error_pm_runtime_enabled:
  1507. pm_runtime_disable(&pdev->dev);
  1508. out_error_clock_enabled:
  1509. clk_disable_unprepare(ssp->clk);
  1510. out_error_dma_irq_alloc:
  1511. pxa2xx_spi_dma_release(drv_data);
  1512. free_irq(ssp->irq, drv_data);
  1513. out_error_master_alloc:
  1514. pxa_ssp_free(ssp);
  1515. return status;
  1516. }
  1517. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1518. {
  1519. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1520. struct ssp_device *ssp;
  1521. if (!drv_data)
  1522. return 0;
  1523. ssp = drv_data->ssp;
  1524. pm_runtime_get_sync(&pdev->dev);
  1525. spi_unregister_controller(drv_data->master);
  1526. /* Disable the SSP at the peripheral and SOC level */
  1527. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1528. clk_disable_unprepare(ssp->clk);
  1529. /* Release DMA */
  1530. if (drv_data->master_info->enable_dma)
  1531. pxa2xx_spi_dma_release(drv_data);
  1532. pm_runtime_put_noidle(&pdev->dev);
  1533. pm_runtime_disable(&pdev->dev);
  1534. /* Release IRQ */
  1535. free_irq(ssp->irq, drv_data);
  1536. /* Release SSP */
  1537. pxa_ssp_free(ssp);
  1538. return 0;
  1539. }
  1540. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1541. {
  1542. int status = 0;
  1543. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1544. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1545. }
  1546. #ifdef CONFIG_PM_SLEEP
  1547. static int pxa2xx_spi_suspend(struct device *dev)
  1548. {
  1549. struct driver_data *drv_data = dev_get_drvdata(dev);
  1550. struct ssp_device *ssp = drv_data->ssp;
  1551. int status;
  1552. status = spi_controller_suspend(drv_data->master);
  1553. if (status != 0)
  1554. return status;
  1555. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1556. if (!pm_runtime_suspended(dev))
  1557. clk_disable_unprepare(ssp->clk);
  1558. return 0;
  1559. }
  1560. static int pxa2xx_spi_resume(struct device *dev)
  1561. {
  1562. struct driver_data *drv_data = dev_get_drvdata(dev);
  1563. struct ssp_device *ssp = drv_data->ssp;
  1564. int status;
  1565. /* Enable the SSP clock */
  1566. if (!pm_runtime_suspended(dev)) {
  1567. status = clk_prepare_enable(ssp->clk);
  1568. if (status)
  1569. return status;
  1570. }
  1571. /* Restore LPSS private register bits */
  1572. if (is_lpss_ssp(drv_data))
  1573. lpss_ssp_setup(drv_data);
  1574. /* Start the queue running */
  1575. status = spi_controller_resume(drv_data->master);
  1576. if (status != 0) {
  1577. dev_err(dev, "problem starting queue (%d)\n", status);
  1578. return status;
  1579. }
  1580. return 0;
  1581. }
  1582. #endif
  1583. #ifdef CONFIG_PM
  1584. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1585. {
  1586. struct driver_data *drv_data = dev_get_drvdata(dev);
  1587. clk_disable_unprepare(drv_data->ssp->clk);
  1588. return 0;
  1589. }
  1590. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1591. {
  1592. struct driver_data *drv_data = dev_get_drvdata(dev);
  1593. int status;
  1594. status = clk_prepare_enable(drv_data->ssp->clk);
  1595. return status;
  1596. }
  1597. #endif
  1598. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1599. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1600. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1601. pxa2xx_spi_runtime_resume, NULL)
  1602. };
  1603. static struct platform_driver driver = {
  1604. .driver = {
  1605. .name = "pxa2xx-spi",
  1606. .pm = &pxa2xx_spi_pm_ops,
  1607. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1608. },
  1609. .probe = pxa2xx_spi_probe,
  1610. .remove = pxa2xx_spi_remove,
  1611. .shutdown = pxa2xx_spi_shutdown,
  1612. };
  1613. static int __init pxa2xx_spi_init(void)
  1614. {
  1615. return platform_driver_register(&driver);
  1616. }
  1617. subsys_initcall(pxa2xx_spi_init);
  1618. static void __exit pxa2xx_spi_exit(void)
  1619. {
  1620. platform_driver_unregister(&driver);
  1621. }
  1622. module_exit(pxa2xx_spi_exit);