pcmcia.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*
  2. * Sonics Silicon Backplane
  3. * PCMCIA-Hostbus related functions
  4. *
  5. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2007-2008 Michael Buesch <m@bues.ch>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/ssb/ssb.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/etherdevice.h>
  14. #include <pcmcia/cistpl.h>
  15. #include <pcmcia/ciscode.h>
  16. #include <pcmcia/ds.h>
  17. #include <pcmcia/cisreg.h>
  18. #include "ssb_private.h"
  19. /* Define the following to 1 to enable a printk on each coreswitch. */
  20. #define SSB_VERBOSE_PCMCIACORESWITCH_DEBUG 0
  21. /* PCMCIA configuration registers */
  22. #define SSB_PCMCIA_ADDRESS0 0x2E
  23. #define SSB_PCMCIA_ADDRESS1 0x30
  24. #define SSB_PCMCIA_ADDRESS2 0x32
  25. #define SSB_PCMCIA_MEMSEG 0x34
  26. #define SSB_PCMCIA_SPROMCTL 0x36
  27. #define SSB_PCMCIA_SPROMCTL_IDLE 0
  28. #define SSB_PCMCIA_SPROMCTL_WRITE 1
  29. #define SSB_PCMCIA_SPROMCTL_READ 2
  30. #define SSB_PCMCIA_SPROMCTL_WRITEEN 4
  31. #define SSB_PCMCIA_SPROMCTL_WRITEDIS 7
  32. #define SSB_PCMCIA_SPROMCTL_DONE 8
  33. #define SSB_PCMCIA_SPROM_DATALO 0x38
  34. #define SSB_PCMCIA_SPROM_DATAHI 0x3A
  35. #define SSB_PCMCIA_SPROM_ADDRLO 0x3C
  36. #define SSB_PCMCIA_SPROM_ADDRHI 0x3E
  37. /* Hardware invariants CIS tuples */
  38. #define SSB_PCMCIA_CIS 0x80
  39. #define SSB_PCMCIA_CIS_ID 0x01
  40. #define SSB_PCMCIA_CIS_BOARDREV 0x02
  41. #define SSB_PCMCIA_CIS_PA 0x03
  42. #define SSB_PCMCIA_CIS_PA_PA0B0_LO 0
  43. #define SSB_PCMCIA_CIS_PA_PA0B0_HI 1
  44. #define SSB_PCMCIA_CIS_PA_PA0B1_LO 2
  45. #define SSB_PCMCIA_CIS_PA_PA0B1_HI 3
  46. #define SSB_PCMCIA_CIS_PA_PA0B2_LO 4
  47. #define SSB_PCMCIA_CIS_PA_PA0B2_HI 5
  48. #define SSB_PCMCIA_CIS_PA_ITSSI 6
  49. #define SSB_PCMCIA_CIS_PA_MAXPOW 7
  50. #define SSB_PCMCIA_CIS_OEMNAME 0x04
  51. #define SSB_PCMCIA_CIS_CCODE 0x05
  52. #define SSB_PCMCIA_CIS_ANTENNA 0x06
  53. #define SSB_PCMCIA_CIS_ANTGAIN 0x07
  54. #define SSB_PCMCIA_CIS_BFLAGS 0x08
  55. #define SSB_PCMCIA_CIS_LEDS 0x09
  56. /* PCMCIA SPROM size. */
  57. #define SSB_PCMCIA_SPROM_SIZE 256
  58. #define SSB_PCMCIA_SPROM_SIZE_BYTES (SSB_PCMCIA_SPROM_SIZE * sizeof(u16))
  59. /* Write to a PCMCIA configuration register. */
  60. static int ssb_pcmcia_cfg_write(struct ssb_bus *bus, u8 offset, u8 value)
  61. {
  62. int res;
  63. res = pcmcia_write_config_byte(bus->host_pcmcia, offset, value);
  64. if (unlikely(res != 0))
  65. return -EBUSY;
  66. return 0;
  67. }
  68. /* Read from a PCMCIA configuration register. */
  69. static int ssb_pcmcia_cfg_read(struct ssb_bus *bus, u8 offset, u8 *value)
  70. {
  71. int res;
  72. res = pcmcia_read_config_byte(bus->host_pcmcia, offset, value);
  73. if (unlikely(res != 0))
  74. return -EBUSY;
  75. return 0;
  76. }
  77. int ssb_pcmcia_switch_coreidx(struct ssb_bus *bus,
  78. u8 coreidx)
  79. {
  80. int err;
  81. int attempts = 0;
  82. u32 cur_core;
  83. u32 addr;
  84. u32 read_addr;
  85. u8 val;
  86. addr = (coreidx * SSB_CORE_SIZE) + SSB_ENUM_BASE;
  87. while (1) {
  88. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS0,
  89. (addr & 0x0000F000) >> 12);
  90. if (err)
  91. goto error;
  92. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS1,
  93. (addr & 0x00FF0000) >> 16);
  94. if (err)
  95. goto error;
  96. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_ADDRESS2,
  97. (addr & 0xFF000000) >> 24);
  98. if (err)
  99. goto error;
  100. read_addr = 0;
  101. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS0, &val);
  102. if (err)
  103. goto error;
  104. read_addr |= ((u32)(val & 0x0F)) << 12;
  105. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS1, &val);
  106. if (err)
  107. goto error;
  108. read_addr |= ((u32)val) << 16;
  109. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_ADDRESS2, &val);
  110. if (err)
  111. goto error;
  112. read_addr |= ((u32)val) << 24;
  113. cur_core = (read_addr - SSB_ENUM_BASE) / SSB_CORE_SIZE;
  114. if (cur_core == coreidx)
  115. break;
  116. err = -ETIMEDOUT;
  117. if (attempts++ > SSB_BAR0_MAX_RETRIES)
  118. goto error;
  119. udelay(10);
  120. }
  121. return 0;
  122. error:
  123. ssb_err("Failed to switch to core %u\n", coreidx);
  124. return err;
  125. }
  126. static int ssb_pcmcia_switch_core(struct ssb_bus *bus, struct ssb_device *dev)
  127. {
  128. int err;
  129. #if SSB_VERBOSE_PCMCIACORESWITCH_DEBUG
  130. ssb_info("Switching to %s core, index %d\n",
  131. ssb_core_name(dev->id.coreid),
  132. dev->core_index);
  133. #endif
  134. err = ssb_pcmcia_switch_coreidx(bus, dev->core_index);
  135. if (!err)
  136. bus->mapped_device = dev;
  137. return err;
  138. }
  139. int ssb_pcmcia_switch_segment(struct ssb_bus *bus, u8 seg)
  140. {
  141. int attempts = 0;
  142. int err;
  143. u8 val;
  144. SSB_WARN_ON((seg != 0) && (seg != 1));
  145. while (1) {
  146. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_MEMSEG, seg);
  147. if (err)
  148. goto error;
  149. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_MEMSEG, &val);
  150. if (err)
  151. goto error;
  152. if (val == seg)
  153. break;
  154. err = -ETIMEDOUT;
  155. if (unlikely(attempts++ > SSB_BAR0_MAX_RETRIES))
  156. goto error;
  157. udelay(10);
  158. }
  159. bus->mapped_pcmcia_seg = seg;
  160. return 0;
  161. error:
  162. ssb_err("Failed to switch pcmcia segment\n");
  163. return err;
  164. }
  165. static int select_core_and_segment(struct ssb_device *dev,
  166. u16 *offset)
  167. {
  168. struct ssb_bus *bus = dev->bus;
  169. int err;
  170. u8 need_segment;
  171. if (*offset >= 0x800) {
  172. *offset -= 0x800;
  173. need_segment = 1;
  174. } else
  175. need_segment = 0;
  176. if (unlikely(dev != bus->mapped_device)) {
  177. err = ssb_pcmcia_switch_core(bus, dev);
  178. if (unlikely(err))
  179. return err;
  180. }
  181. if (unlikely(need_segment != bus->mapped_pcmcia_seg)) {
  182. err = ssb_pcmcia_switch_segment(bus, need_segment);
  183. if (unlikely(err))
  184. return err;
  185. }
  186. return 0;
  187. }
  188. static u8 ssb_pcmcia_read8(struct ssb_device *dev, u16 offset)
  189. {
  190. struct ssb_bus *bus = dev->bus;
  191. unsigned long flags;
  192. int err;
  193. u8 value = 0xFF;
  194. spin_lock_irqsave(&bus->bar_lock, flags);
  195. err = select_core_and_segment(dev, &offset);
  196. if (likely(!err))
  197. value = readb(bus->mmio + offset);
  198. spin_unlock_irqrestore(&bus->bar_lock, flags);
  199. return value;
  200. }
  201. static u16 ssb_pcmcia_read16(struct ssb_device *dev, u16 offset)
  202. {
  203. struct ssb_bus *bus = dev->bus;
  204. unsigned long flags;
  205. int err;
  206. u16 value = 0xFFFF;
  207. spin_lock_irqsave(&bus->bar_lock, flags);
  208. err = select_core_and_segment(dev, &offset);
  209. if (likely(!err))
  210. value = readw(bus->mmio + offset);
  211. spin_unlock_irqrestore(&bus->bar_lock, flags);
  212. return value;
  213. }
  214. static u32 ssb_pcmcia_read32(struct ssb_device *dev, u16 offset)
  215. {
  216. struct ssb_bus *bus = dev->bus;
  217. unsigned long flags;
  218. int err;
  219. u32 lo = 0xFFFFFFFF, hi = 0xFFFFFFFF;
  220. spin_lock_irqsave(&bus->bar_lock, flags);
  221. err = select_core_and_segment(dev, &offset);
  222. if (likely(!err)) {
  223. lo = readw(bus->mmio + offset);
  224. hi = readw(bus->mmio + offset + 2);
  225. }
  226. spin_unlock_irqrestore(&bus->bar_lock, flags);
  227. return (lo | (hi << 16));
  228. }
  229. #ifdef CONFIG_SSB_BLOCKIO
  230. static void ssb_pcmcia_block_read(struct ssb_device *dev, void *buffer,
  231. size_t count, u16 offset, u8 reg_width)
  232. {
  233. struct ssb_bus *bus = dev->bus;
  234. unsigned long flags;
  235. void __iomem *addr = bus->mmio + offset;
  236. int err;
  237. spin_lock_irqsave(&bus->bar_lock, flags);
  238. err = select_core_and_segment(dev, &offset);
  239. if (unlikely(err)) {
  240. memset(buffer, 0xFF, count);
  241. goto unlock;
  242. }
  243. switch (reg_width) {
  244. case sizeof(u8): {
  245. u8 *buf = buffer;
  246. while (count) {
  247. *buf = __raw_readb(addr);
  248. buf++;
  249. count--;
  250. }
  251. break;
  252. }
  253. case sizeof(u16): {
  254. __le16 *buf = buffer;
  255. SSB_WARN_ON(count & 1);
  256. while (count) {
  257. *buf = (__force __le16)__raw_readw(addr);
  258. buf++;
  259. count -= 2;
  260. }
  261. break;
  262. }
  263. case sizeof(u32): {
  264. __le16 *buf = buffer;
  265. SSB_WARN_ON(count & 3);
  266. while (count) {
  267. *buf = (__force __le16)__raw_readw(addr);
  268. buf++;
  269. *buf = (__force __le16)__raw_readw(addr + 2);
  270. buf++;
  271. count -= 4;
  272. }
  273. break;
  274. }
  275. default:
  276. SSB_WARN_ON(1);
  277. }
  278. unlock:
  279. spin_unlock_irqrestore(&bus->bar_lock, flags);
  280. }
  281. #endif /* CONFIG_SSB_BLOCKIO */
  282. static void ssb_pcmcia_write8(struct ssb_device *dev, u16 offset, u8 value)
  283. {
  284. struct ssb_bus *bus = dev->bus;
  285. unsigned long flags;
  286. int err;
  287. spin_lock_irqsave(&bus->bar_lock, flags);
  288. err = select_core_and_segment(dev, &offset);
  289. if (likely(!err))
  290. writeb(value, bus->mmio + offset);
  291. mmiowb();
  292. spin_unlock_irqrestore(&bus->bar_lock, flags);
  293. }
  294. static void ssb_pcmcia_write16(struct ssb_device *dev, u16 offset, u16 value)
  295. {
  296. struct ssb_bus *bus = dev->bus;
  297. unsigned long flags;
  298. int err;
  299. spin_lock_irqsave(&bus->bar_lock, flags);
  300. err = select_core_and_segment(dev, &offset);
  301. if (likely(!err))
  302. writew(value, bus->mmio + offset);
  303. mmiowb();
  304. spin_unlock_irqrestore(&bus->bar_lock, flags);
  305. }
  306. static void ssb_pcmcia_write32(struct ssb_device *dev, u16 offset, u32 value)
  307. {
  308. struct ssb_bus *bus = dev->bus;
  309. unsigned long flags;
  310. int err;
  311. spin_lock_irqsave(&bus->bar_lock, flags);
  312. err = select_core_and_segment(dev, &offset);
  313. if (likely(!err)) {
  314. writew((value & 0x0000FFFF), bus->mmio + offset);
  315. writew(((value & 0xFFFF0000) >> 16), bus->mmio + offset + 2);
  316. }
  317. mmiowb();
  318. spin_unlock_irqrestore(&bus->bar_lock, flags);
  319. }
  320. #ifdef CONFIG_SSB_BLOCKIO
  321. static void ssb_pcmcia_block_write(struct ssb_device *dev, const void *buffer,
  322. size_t count, u16 offset, u8 reg_width)
  323. {
  324. struct ssb_bus *bus = dev->bus;
  325. unsigned long flags;
  326. void __iomem *addr = bus->mmio + offset;
  327. int err;
  328. spin_lock_irqsave(&bus->bar_lock, flags);
  329. err = select_core_and_segment(dev, &offset);
  330. if (unlikely(err))
  331. goto unlock;
  332. switch (reg_width) {
  333. case sizeof(u8): {
  334. const u8 *buf = buffer;
  335. while (count) {
  336. __raw_writeb(*buf, addr);
  337. buf++;
  338. count--;
  339. }
  340. break;
  341. }
  342. case sizeof(u16): {
  343. const __le16 *buf = buffer;
  344. SSB_WARN_ON(count & 1);
  345. while (count) {
  346. __raw_writew((__force u16)(*buf), addr);
  347. buf++;
  348. count -= 2;
  349. }
  350. break;
  351. }
  352. case sizeof(u32): {
  353. const __le16 *buf = buffer;
  354. SSB_WARN_ON(count & 3);
  355. while (count) {
  356. __raw_writew((__force u16)(*buf), addr);
  357. buf++;
  358. __raw_writew((__force u16)(*buf), addr + 2);
  359. buf++;
  360. count -= 4;
  361. }
  362. break;
  363. }
  364. default:
  365. SSB_WARN_ON(1);
  366. }
  367. unlock:
  368. mmiowb();
  369. spin_unlock_irqrestore(&bus->bar_lock, flags);
  370. }
  371. #endif /* CONFIG_SSB_BLOCKIO */
  372. /* Not "static", as it's used in main.c */
  373. const struct ssb_bus_ops ssb_pcmcia_ops = {
  374. .read8 = ssb_pcmcia_read8,
  375. .read16 = ssb_pcmcia_read16,
  376. .read32 = ssb_pcmcia_read32,
  377. .write8 = ssb_pcmcia_write8,
  378. .write16 = ssb_pcmcia_write16,
  379. .write32 = ssb_pcmcia_write32,
  380. #ifdef CONFIG_SSB_BLOCKIO
  381. .block_read = ssb_pcmcia_block_read,
  382. .block_write = ssb_pcmcia_block_write,
  383. #endif
  384. };
  385. static int ssb_pcmcia_sprom_command(struct ssb_bus *bus, u8 command)
  386. {
  387. unsigned int i;
  388. int err;
  389. u8 value;
  390. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROMCTL, command);
  391. if (err)
  392. return err;
  393. for (i = 0; i < 1000; i++) {
  394. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROMCTL, &value);
  395. if (err)
  396. return err;
  397. if (value & SSB_PCMCIA_SPROMCTL_DONE)
  398. return 0;
  399. udelay(10);
  400. }
  401. return -ETIMEDOUT;
  402. }
  403. /* offset is the 16bit word offset */
  404. static int ssb_pcmcia_sprom_read(struct ssb_bus *bus, u16 offset, u16 *value)
  405. {
  406. int err;
  407. u8 lo, hi;
  408. offset *= 2; /* Make byte offset */
  409. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
  410. (offset & 0x00FF));
  411. if (err)
  412. return err;
  413. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
  414. (offset & 0xFF00) >> 8);
  415. if (err)
  416. return err;
  417. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_READ);
  418. if (err)
  419. return err;
  420. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATALO, &lo);
  421. if (err)
  422. return err;
  423. err = ssb_pcmcia_cfg_read(bus, SSB_PCMCIA_SPROM_DATAHI, &hi);
  424. if (err)
  425. return err;
  426. *value = (lo | (((u16)hi) << 8));
  427. return 0;
  428. }
  429. /* offset is the 16bit word offset */
  430. static int ssb_pcmcia_sprom_write(struct ssb_bus *bus, u16 offset, u16 value)
  431. {
  432. int err;
  433. offset *= 2; /* Make byte offset */
  434. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRLO,
  435. (offset & 0x00FF));
  436. if (err)
  437. return err;
  438. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_ADDRHI,
  439. (offset & 0xFF00) >> 8);
  440. if (err)
  441. return err;
  442. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATALO,
  443. (value & 0x00FF));
  444. if (err)
  445. return err;
  446. err = ssb_pcmcia_cfg_write(bus, SSB_PCMCIA_SPROM_DATAHI,
  447. (value & 0xFF00) >> 8);
  448. if (err)
  449. return err;
  450. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITE);
  451. if (err)
  452. return err;
  453. msleep(20);
  454. return 0;
  455. }
  456. /* Read the SPROM image. bufsize is in 16bit words. */
  457. static int ssb_pcmcia_sprom_read_all(struct ssb_bus *bus, u16 *sprom)
  458. {
  459. int err, i;
  460. for (i = 0; i < SSB_PCMCIA_SPROM_SIZE; i++) {
  461. err = ssb_pcmcia_sprom_read(bus, i, &sprom[i]);
  462. if (err)
  463. return err;
  464. }
  465. return 0;
  466. }
  467. /* Write the SPROM image. size is in 16bit words. */
  468. static int ssb_pcmcia_sprom_write_all(struct ssb_bus *bus, const u16 *sprom)
  469. {
  470. int i, err;
  471. bool failed = 0;
  472. size_t size = SSB_PCMCIA_SPROM_SIZE;
  473. ssb_notice("Writing SPROM. Do NOT turn off the power! Please stand by...\n");
  474. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEEN);
  475. if (err) {
  476. ssb_notice("Could not enable SPROM write access\n");
  477. return -EBUSY;
  478. }
  479. ssb_notice("[ 0%%");
  480. msleep(500);
  481. for (i = 0; i < size; i++) {
  482. if (i == size / 4)
  483. ssb_cont("25%%");
  484. else if (i == size / 2)
  485. ssb_cont("50%%");
  486. else if (i == (size * 3) / 4)
  487. ssb_cont("75%%");
  488. else if (i % 2)
  489. ssb_cont(".");
  490. err = ssb_pcmcia_sprom_write(bus, i, sprom[i]);
  491. if (err) {
  492. ssb_notice("Failed to write to SPROM\n");
  493. failed = 1;
  494. break;
  495. }
  496. }
  497. err = ssb_pcmcia_sprom_command(bus, SSB_PCMCIA_SPROMCTL_WRITEDIS);
  498. if (err) {
  499. ssb_notice("Could not disable SPROM write access\n");
  500. failed = 1;
  501. }
  502. msleep(500);
  503. if (!failed) {
  504. ssb_cont("100%% ]\n");
  505. ssb_notice("SPROM written\n");
  506. }
  507. return failed ? -EBUSY : 0;
  508. }
  509. static int ssb_pcmcia_sprom_check_crc(const u16 *sprom, size_t size)
  510. {
  511. //TODO
  512. return 0;
  513. }
  514. #define GOTO_ERROR_ON(condition, description) do { \
  515. if (unlikely(condition)) { \
  516. error_description = description; \
  517. goto error; \
  518. } \
  519. } while (0)
  520. static int ssb_pcmcia_get_mac(struct pcmcia_device *p_dev,
  521. tuple_t *tuple,
  522. void *priv)
  523. {
  524. struct ssb_sprom *sprom = priv;
  525. if (tuple->TupleData[0] != CISTPL_FUNCE_LAN_NODE_ID)
  526. return -EINVAL;
  527. if (tuple->TupleDataLen != ETH_ALEN + 2)
  528. return -EINVAL;
  529. if (tuple->TupleData[1] != ETH_ALEN)
  530. return -EINVAL;
  531. memcpy(sprom->il0mac, &tuple->TupleData[2], ETH_ALEN);
  532. return 0;
  533. };
  534. static int ssb_pcmcia_do_get_invariants(struct pcmcia_device *p_dev,
  535. tuple_t *tuple,
  536. void *priv)
  537. {
  538. struct ssb_init_invariants *iv = priv;
  539. struct ssb_sprom *sprom = &iv->sprom;
  540. struct ssb_boardinfo *bi = &iv->boardinfo;
  541. const char *error_description;
  542. GOTO_ERROR_ON(tuple->TupleDataLen < 1, "VEN tpl < 1");
  543. switch (tuple->TupleData[0]) {
  544. case SSB_PCMCIA_CIS_ID:
  545. GOTO_ERROR_ON((tuple->TupleDataLen != 5) &&
  546. (tuple->TupleDataLen != 7),
  547. "id tpl size");
  548. bi->vendor = tuple->TupleData[1] |
  549. ((u16)tuple->TupleData[2] << 8);
  550. break;
  551. case SSB_PCMCIA_CIS_BOARDREV:
  552. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  553. "boardrev tpl size");
  554. sprom->board_rev = tuple->TupleData[1];
  555. break;
  556. case SSB_PCMCIA_CIS_PA:
  557. GOTO_ERROR_ON((tuple->TupleDataLen != 9) &&
  558. (tuple->TupleDataLen != 10),
  559. "pa tpl size");
  560. sprom->pa0b0 = tuple->TupleData[1] |
  561. ((u16)tuple->TupleData[2] << 8);
  562. sprom->pa0b1 = tuple->TupleData[3] |
  563. ((u16)tuple->TupleData[4] << 8);
  564. sprom->pa0b2 = tuple->TupleData[5] |
  565. ((u16)tuple->TupleData[6] << 8);
  566. sprom->itssi_a = tuple->TupleData[7];
  567. sprom->itssi_bg = tuple->TupleData[7];
  568. sprom->maxpwr_a = tuple->TupleData[8];
  569. sprom->maxpwr_bg = tuple->TupleData[8];
  570. break;
  571. case SSB_PCMCIA_CIS_OEMNAME:
  572. /* We ignore this. */
  573. break;
  574. case SSB_PCMCIA_CIS_CCODE:
  575. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  576. "ccode tpl size");
  577. sprom->country_code = tuple->TupleData[1];
  578. break;
  579. case SSB_PCMCIA_CIS_ANTENNA:
  580. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  581. "ant tpl size");
  582. sprom->ant_available_a = tuple->TupleData[1];
  583. sprom->ant_available_bg = tuple->TupleData[1];
  584. break;
  585. case SSB_PCMCIA_CIS_ANTGAIN:
  586. GOTO_ERROR_ON(tuple->TupleDataLen != 2,
  587. "antg tpl size");
  588. sprom->antenna_gain.a0 = tuple->TupleData[1];
  589. sprom->antenna_gain.a1 = tuple->TupleData[1];
  590. sprom->antenna_gain.a2 = tuple->TupleData[1];
  591. sprom->antenna_gain.a3 = tuple->TupleData[1];
  592. break;
  593. case SSB_PCMCIA_CIS_BFLAGS:
  594. GOTO_ERROR_ON((tuple->TupleDataLen != 3) &&
  595. (tuple->TupleDataLen != 5),
  596. "bfl tpl size");
  597. sprom->boardflags_lo = tuple->TupleData[1] |
  598. ((u16)tuple->TupleData[2] << 8);
  599. break;
  600. case SSB_PCMCIA_CIS_LEDS:
  601. GOTO_ERROR_ON(tuple->TupleDataLen != 5,
  602. "leds tpl size");
  603. sprom->gpio0 = tuple->TupleData[1];
  604. sprom->gpio1 = tuple->TupleData[2];
  605. sprom->gpio2 = tuple->TupleData[3];
  606. sprom->gpio3 = tuple->TupleData[4];
  607. break;
  608. }
  609. return -ENOSPC; /* continue with next entry */
  610. error:
  611. ssb_err(
  612. "PCMCIA: Failed to fetch device invariants: %s\n",
  613. error_description);
  614. return -ENODEV;
  615. }
  616. int ssb_pcmcia_get_invariants(struct ssb_bus *bus,
  617. struct ssb_init_invariants *iv)
  618. {
  619. struct ssb_sprom *sprom = &iv->sprom;
  620. int res;
  621. memset(sprom, 0xFF, sizeof(*sprom));
  622. sprom->revision = 1;
  623. sprom->boardflags_lo = 0;
  624. sprom->boardflags_hi = 0;
  625. /* First fetch the MAC address. */
  626. res = pcmcia_loop_tuple(bus->host_pcmcia, CISTPL_FUNCE,
  627. ssb_pcmcia_get_mac, sprom);
  628. if (res != 0) {
  629. ssb_err(
  630. "PCMCIA: Failed to fetch MAC address\n");
  631. return -ENODEV;
  632. }
  633. /* Fetch the vendor specific tuples. */
  634. res = pcmcia_loop_tuple(bus->host_pcmcia, SSB_PCMCIA_CIS,
  635. ssb_pcmcia_do_get_invariants, iv);
  636. if ((res == 0) || (res == -ENOSPC))
  637. return 0;
  638. ssb_err(
  639. "PCMCIA: Failed to fetch device invariants\n");
  640. return -ENODEV;
  641. }
  642. static ssize_t ssb_pcmcia_attr_sprom_show(struct device *pcmciadev,
  643. struct device_attribute *attr,
  644. char *buf)
  645. {
  646. struct pcmcia_device *pdev =
  647. container_of(pcmciadev, struct pcmcia_device, dev);
  648. struct ssb_bus *bus;
  649. bus = ssb_pcmcia_dev_to_bus(pdev);
  650. if (!bus)
  651. return -ENODEV;
  652. return ssb_attr_sprom_show(bus, buf,
  653. ssb_pcmcia_sprom_read_all);
  654. }
  655. static ssize_t ssb_pcmcia_attr_sprom_store(struct device *pcmciadev,
  656. struct device_attribute *attr,
  657. const char *buf, size_t count)
  658. {
  659. struct pcmcia_device *pdev =
  660. container_of(pcmciadev, struct pcmcia_device, dev);
  661. struct ssb_bus *bus;
  662. bus = ssb_pcmcia_dev_to_bus(pdev);
  663. if (!bus)
  664. return -ENODEV;
  665. return ssb_attr_sprom_store(bus, buf, count,
  666. ssb_pcmcia_sprom_check_crc,
  667. ssb_pcmcia_sprom_write_all);
  668. }
  669. static DEVICE_ATTR(ssb_sprom, 0600,
  670. ssb_pcmcia_attr_sprom_show,
  671. ssb_pcmcia_attr_sprom_store);
  672. static int ssb_pcmcia_cor_setup(struct ssb_bus *bus, u8 cor)
  673. {
  674. u8 val;
  675. int err;
  676. err = ssb_pcmcia_cfg_read(bus, cor, &val);
  677. if (err)
  678. return err;
  679. val &= ~COR_SOFT_RESET;
  680. val |= COR_FUNC_ENA | COR_IREQ_ENA | COR_LEVEL_REQ;
  681. err = ssb_pcmcia_cfg_write(bus, cor, val);
  682. if (err)
  683. return err;
  684. msleep(40);
  685. return 0;
  686. }
  687. /* Initialize the PCMCIA hardware. This is called on Init and Resume. */
  688. int ssb_pcmcia_hardware_setup(struct ssb_bus *bus)
  689. {
  690. int err;
  691. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  692. return 0;
  693. /* Switch segment to a known state and sync
  694. * bus->mapped_pcmcia_seg with hardware state. */
  695. ssb_pcmcia_switch_segment(bus, 0);
  696. /* Init the COR register. */
  697. err = ssb_pcmcia_cor_setup(bus, CISREG_COR);
  698. if (err)
  699. return err;
  700. /* Some cards also need this register to get poked. */
  701. err = ssb_pcmcia_cor_setup(bus, CISREG_COR + 0x80);
  702. if (err)
  703. return err;
  704. return 0;
  705. }
  706. void ssb_pcmcia_exit(struct ssb_bus *bus)
  707. {
  708. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  709. return;
  710. device_remove_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
  711. }
  712. int ssb_pcmcia_init(struct ssb_bus *bus)
  713. {
  714. int err;
  715. if (bus->bustype != SSB_BUSTYPE_PCMCIA)
  716. return 0;
  717. err = ssb_pcmcia_hardware_setup(bus);
  718. if (err)
  719. goto error;
  720. bus->sprom_size = SSB_PCMCIA_SPROM_SIZE;
  721. mutex_init(&bus->sprom_mutex);
  722. err = device_create_file(&bus->host_pcmcia->dev, &dev_attr_ssb_sprom);
  723. if (err)
  724. goto error;
  725. return 0;
  726. error:
  727. ssb_err("Failed to initialize PCMCIA host device\n");
  728. return err;
  729. }