ixp4xx_hss.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Intel IXP4xx HSS (synchronous serial port) driver for Linux
  3. *
  4. * Copyright (C) 2007-2008 Krzysztof Hałasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/cdev.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/fs.h>
  15. #include <linux/hdlc.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/poll.h>
  20. #include <linux/slab.h>
  21. #include <mach/npe.h>
  22. #include <mach/qmgr.h>
  23. #define DEBUG_DESC 0
  24. #define DEBUG_RX 0
  25. #define DEBUG_TX 0
  26. #define DEBUG_PKT_BYTES 0
  27. #define DEBUG_CLOSE 0
  28. #define DRV_NAME "ixp4xx_hss"
  29. #define PKT_EXTRA_FLAGS 0 /* orig 1 */
  30. #define PKT_NUM_PIPES 1 /* 1, 2 or 4 */
  31. #define PKT_PIPE_FIFO_SIZEW 4 /* total 4 dwords per HSS */
  32. #define RX_DESCS 16 /* also length of all RX queues */
  33. #define TX_DESCS 16 /* also length of all TX queues */
  34. #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
  35. #define RX_SIZE (HDLC_MAX_MRU + 4) /* NPE needs more space */
  36. #define MAX_CLOSE_WAIT 1000 /* microseconds */
  37. #define HSS_COUNT 2
  38. #define FRAME_SIZE 256 /* doesn't matter at this point */
  39. #define FRAME_OFFSET 0
  40. #define MAX_CHANNELS (FRAME_SIZE / 8)
  41. #define NAPI_WEIGHT 16
  42. /* Queue IDs */
  43. #define HSS0_CHL_RXTRIG_QUEUE 12 /* orig size = 32 dwords */
  44. #define HSS0_PKT_RX_QUEUE 13 /* orig size = 32 dwords */
  45. #define HSS0_PKT_TX0_QUEUE 14 /* orig size = 16 dwords */
  46. #define HSS0_PKT_TX1_QUEUE 15
  47. #define HSS0_PKT_TX2_QUEUE 16
  48. #define HSS0_PKT_TX3_QUEUE 17
  49. #define HSS0_PKT_RXFREE0_QUEUE 18 /* orig size = 16 dwords */
  50. #define HSS0_PKT_RXFREE1_QUEUE 19
  51. #define HSS0_PKT_RXFREE2_QUEUE 20
  52. #define HSS0_PKT_RXFREE3_QUEUE 21
  53. #define HSS0_PKT_TXDONE_QUEUE 22 /* orig size = 64 dwords */
  54. #define HSS1_CHL_RXTRIG_QUEUE 10
  55. #define HSS1_PKT_RX_QUEUE 0
  56. #define HSS1_PKT_TX0_QUEUE 5
  57. #define HSS1_PKT_TX1_QUEUE 6
  58. #define HSS1_PKT_TX2_QUEUE 7
  59. #define HSS1_PKT_TX3_QUEUE 8
  60. #define HSS1_PKT_RXFREE0_QUEUE 1
  61. #define HSS1_PKT_RXFREE1_QUEUE 2
  62. #define HSS1_PKT_RXFREE2_QUEUE 3
  63. #define HSS1_PKT_RXFREE3_QUEUE 4
  64. #define HSS1_PKT_TXDONE_QUEUE 9
  65. #define NPE_PKT_MODE_HDLC 0
  66. #define NPE_PKT_MODE_RAW 1
  67. #define NPE_PKT_MODE_56KMODE 2
  68. #define NPE_PKT_MODE_56KENDIAN_MSB 4
  69. /* PKT_PIPE_HDLC_CFG_WRITE flags */
  70. #define PKT_HDLC_IDLE_ONES 0x1 /* default = flags */
  71. #define PKT_HDLC_CRC_32 0x2 /* default = CRC-16 */
  72. #define PKT_HDLC_MSB_ENDIAN 0x4 /* default = LE */
  73. /* hss_config, PCRs */
  74. /* Frame sync sampling, default = active low */
  75. #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
  76. #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
  77. #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
  78. /* Frame sync pin: input (default) or output generated off a given clk edge */
  79. #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
  80. #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
  81. /* Frame and data clock sampling on edge, default = falling */
  82. #define PCR_FCLK_EDGE_RISING 0x08000000
  83. #define PCR_DCLK_EDGE_RISING 0x04000000
  84. /* Clock direction, default = input */
  85. #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
  86. /* Generate/Receive frame pulses, default = enabled */
  87. #define PCR_FRM_PULSE_DISABLED 0x01000000
  88. /* Data rate is full (default) or half the configured clk speed */
  89. #define PCR_HALF_CLK_RATE 0x00200000
  90. /* Invert data between NPE and HSS FIFOs? (default = no) */
  91. #define PCR_DATA_POLARITY_INVERT 0x00100000
  92. /* TX/RX endianness, default = LSB */
  93. #define PCR_MSB_ENDIAN 0x00080000
  94. /* Normal (default) / open drain mode (TX only) */
  95. #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
  96. /* No framing bit transmitted and expected on RX? (default = framing bit) */
  97. #define PCR_SOF_NO_FBIT 0x00020000
  98. /* Drive data pins? */
  99. #define PCR_TX_DATA_ENABLE 0x00010000
  100. /* Voice 56k type: drive the data pins low (default), high, high Z */
  101. #define PCR_TX_V56K_HIGH 0x00002000
  102. #define PCR_TX_V56K_HIGH_IMP 0x00004000
  103. /* Unassigned type: drive the data pins low (default), high, high Z */
  104. #define PCR_TX_UNASS_HIGH 0x00000800
  105. #define PCR_TX_UNASS_HIGH_IMP 0x00001000
  106. /* T1 @ 1.544MHz only: Fbit dictated in FIFO (default) or high Z */
  107. #define PCR_TX_FB_HIGH_IMP 0x00000400
  108. /* 56k data endiannes - which bit unused: high (default) or low */
  109. #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
  110. /* 56k data transmission type: 32/8 bit data (default) or 56K data */
  111. #define PCR_TX_56KS_56K_DATA 0x00000100
  112. /* hss_config, cCR */
  113. /* Number of packetized clients, default = 1 */
  114. #define CCR_NPE_HFIFO_2_HDLC 0x04000000
  115. #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
  116. /* default = no loopback */
  117. #define CCR_LOOPBACK 0x02000000
  118. /* HSS number, default = 0 (first) */
  119. #define CCR_SECOND_HSS 0x01000000
  120. /* hss_config, clkCR: main:10, num:10, denom:12 */
  121. #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15) /*65 KHz*/
  122. #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
  123. #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
  124. #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
  125. #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
  126. #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
  127. #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
  128. #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
  129. #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
  130. #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
  131. #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
  132. #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
  133. #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
  134. /*
  135. * HSS_CONFIG_CLOCK_CR register consists of 3 parts:
  136. * A (10 bits), B (10 bits) and C (12 bits).
  137. * IXP42x HSS clock generator operation (verified with an oscilloscope):
  138. * Each clock bit takes 7.5 ns (1 / 133.xx MHz).
  139. * The clock sequence consists of (C - B) states of 0s and 1s, each state is
  140. * A bits wide. It's followed by (B + 1) states of 0s and 1s, each state is
  141. * (A + 1) bits wide.
  142. *
  143. * The resulting average clock frequency (assuming 33.333 MHz oscillator) is:
  144. * freq = 66.666 MHz / (A + (B + 1) / (C + 1))
  145. * minimum freq = 66.666 MHz / (A + 1)
  146. * maximum freq = 66.666 MHz / A
  147. *
  148. * Example: A = 2, B = 2, C = 7, CLOCK_CR register = 2 << 22 | 2 << 12 | 7
  149. * freq = 66.666 MHz / (2 + (2 + 1) / (7 + 1)) = 28.07 MHz (Mb/s).
  150. * The clock sequence is: 1100110011 (5 doubles) 000111000 (3 triples).
  151. * The sequence takes (C - B) * A + (B + 1) * (A + 1) = 5 * 2 + 3 * 3 bits
  152. * = 19 bits (each 7.5 ns long) = 142.5 ns (then the sequence repeats).
  153. * The sequence consists of 4 complete clock periods, thus the average
  154. * frequency (= clock rate) is 4 / 142.5 ns = 28.07 MHz (Mb/s).
  155. * (max specified clock rate for IXP42x HSS is 8.192 Mb/s).
  156. */
  157. /* hss_config, LUT entries */
  158. #define TDMMAP_UNASSIGNED 0
  159. #define TDMMAP_HDLC 1 /* HDLC - packetized */
  160. #define TDMMAP_VOICE56K 2 /* Voice56K - 7-bit channelized */
  161. #define TDMMAP_VOICE64K 3 /* Voice64K - 8-bit channelized */
  162. /* offsets into HSS config */
  163. #define HSS_CONFIG_TX_PCR 0x00 /* port configuration registers */
  164. #define HSS_CONFIG_RX_PCR 0x04
  165. #define HSS_CONFIG_CORE_CR 0x08 /* loopback control, HSS# */
  166. #define HSS_CONFIG_CLOCK_CR 0x0C /* clock generator control */
  167. #define HSS_CONFIG_TX_FCR 0x10 /* frame configuration registers */
  168. #define HSS_CONFIG_RX_FCR 0x14
  169. #define HSS_CONFIG_TX_LUT 0x18 /* channel look-up tables */
  170. #define HSS_CONFIG_RX_LUT 0x38
  171. /* NPE command codes */
  172. /* writes the ConfigWord value to the location specified by offset */
  173. #define PORT_CONFIG_WRITE 0x40
  174. /* triggers the NPE to load the contents of the configuration table */
  175. #define PORT_CONFIG_LOAD 0x41
  176. /* triggers the NPE to return an HssErrorReadResponse message */
  177. #define PORT_ERROR_READ 0x42
  178. /* triggers the NPE to reset internal status and enable the HssPacketized
  179. operation for the flow specified by pPipe */
  180. #define PKT_PIPE_FLOW_ENABLE 0x50
  181. #define PKT_PIPE_FLOW_DISABLE 0x51
  182. #define PKT_NUM_PIPES_WRITE 0x52
  183. #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
  184. #define PKT_PIPE_HDLC_CFG_WRITE 0x54
  185. #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
  186. #define PKT_PIPE_RX_SIZE_WRITE 0x56
  187. #define PKT_PIPE_MODE_WRITE 0x57
  188. /* HDLC packet status values - desc->status */
  189. #define ERR_SHUTDOWN 1 /* stop or shutdown occurrence */
  190. #define ERR_HDLC_ALIGN 2 /* HDLC alignment error */
  191. #define ERR_HDLC_FCS 3 /* HDLC Frame Check Sum error */
  192. #define ERR_RXFREE_Q_EMPTY 4 /* RX-free queue became empty while receiving
  193. this packet (if buf_len < pkt_len) */
  194. #define ERR_HDLC_TOO_LONG 5 /* HDLC frame size too long */
  195. #define ERR_HDLC_ABORT 6 /* abort sequence received */
  196. #define ERR_DISCONNECTING 7 /* disconnect is in progress */
  197. #ifdef __ARMEB__
  198. typedef struct sk_buff buffer_t;
  199. #define free_buffer dev_kfree_skb
  200. #define free_buffer_irq dev_kfree_skb_irq
  201. #else
  202. typedef void buffer_t;
  203. #define free_buffer kfree
  204. #define free_buffer_irq kfree
  205. #endif
  206. struct port {
  207. struct device *dev;
  208. struct npe *npe;
  209. struct net_device *netdev;
  210. struct napi_struct napi;
  211. struct hss_plat_info *plat;
  212. buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
  213. struct desc *desc_tab; /* coherent */
  214. u32 desc_tab_phys;
  215. unsigned int id;
  216. unsigned int clock_type, clock_rate, loopback;
  217. unsigned int initialized, carrier;
  218. u8 hdlc_cfg;
  219. u32 clock_reg;
  220. };
  221. /* NPE message structure */
  222. struct msg {
  223. #ifdef __ARMEB__
  224. u8 cmd, unused, hss_port, index;
  225. union {
  226. struct { u8 data8a, data8b, data8c, data8d; };
  227. struct { u16 data16a, data16b; };
  228. struct { u32 data32; };
  229. };
  230. #else
  231. u8 index, hss_port, unused, cmd;
  232. union {
  233. struct { u8 data8d, data8c, data8b, data8a; };
  234. struct { u16 data16b, data16a; };
  235. struct { u32 data32; };
  236. };
  237. #endif
  238. };
  239. /* HDLC packet descriptor */
  240. struct desc {
  241. u32 next; /* pointer to next buffer, unused */
  242. #ifdef __ARMEB__
  243. u16 buf_len; /* buffer length */
  244. u16 pkt_len; /* packet length */
  245. u32 data; /* pointer to data buffer in RAM */
  246. u8 status;
  247. u8 error_count;
  248. u16 __reserved;
  249. #else
  250. u16 pkt_len; /* packet length */
  251. u16 buf_len; /* buffer length */
  252. u32 data; /* pointer to data buffer in RAM */
  253. u16 __reserved;
  254. u8 error_count;
  255. u8 status;
  256. #endif
  257. u32 __reserved1[4];
  258. };
  259. #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
  260. (n) * sizeof(struct desc))
  261. #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
  262. #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
  263. ((n) + RX_DESCS) * sizeof(struct desc))
  264. #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
  265. /*****************************************************************************
  266. * global variables
  267. ****************************************************************************/
  268. static int ports_open;
  269. static struct dma_pool *dma_pool;
  270. static spinlock_t npe_lock;
  271. static const struct {
  272. int tx, txdone, rx, rxfree;
  273. }queue_ids[2] = {{HSS0_PKT_TX0_QUEUE, HSS0_PKT_TXDONE_QUEUE, HSS0_PKT_RX_QUEUE,
  274. HSS0_PKT_RXFREE0_QUEUE},
  275. {HSS1_PKT_TX0_QUEUE, HSS1_PKT_TXDONE_QUEUE, HSS1_PKT_RX_QUEUE,
  276. HSS1_PKT_RXFREE0_QUEUE},
  277. };
  278. /*****************************************************************************
  279. * utility functions
  280. ****************************************************************************/
  281. static inline struct port* dev_to_port(struct net_device *dev)
  282. {
  283. return dev_to_hdlc(dev)->priv;
  284. }
  285. #ifndef __ARMEB__
  286. static inline void memcpy_swab32(u32 *dest, u32 *src, int cnt)
  287. {
  288. int i;
  289. for (i = 0; i < cnt; i++)
  290. dest[i] = swab32(src[i]);
  291. }
  292. #endif
  293. /*****************************************************************************
  294. * HSS access
  295. ****************************************************************************/
  296. static void hss_npe_send(struct port *port, struct msg *msg, const char* what)
  297. {
  298. u32 *val = (u32*)msg;
  299. if (npe_send_message(port->npe, msg, what)) {
  300. printk(KERN_CRIT "HSS-%i: unable to send command [%08X:%08X]"
  301. " to %s\n", port->id, val[0], val[1],
  302. npe_name(port->npe));
  303. BUG();
  304. }
  305. }
  306. static void hss_config_set_lut(struct port *port)
  307. {
  308. struct msg msg;
  309. int ch;
  310. memset(&msg, 0, sizeof(msg));
  311. msg.cmd = PORT_CONFIG_WRITE;
  312. msg.hss_port = port->id;
  313. for (ch = 0; ch < MAX_CHANNELS; ch++) {
  314. msg.data32 >>= 2;
  315. msg.data32 |= TDMMAP_HDLC << 30;
  316. if (ch % 16 == 15) {
  317. msg.index = HSS_CONFIG_TX_LUT + ((ch / 4) & ~3);
  318. hss_npe_send(port, &msg, "HSS_SET_TX_LUT");
  319. msg.index += HSS_CONFIG_RX_LUT - HSS_CONFIG_TX_LUT;
  320. hss_npe_send(port, &msg, "HSS_SET_RX_LUT");
  321. }
  322. }
  323. }
  324. static void hss_config(struct port *port)
  325. {
  326. struct msg msg;
  327. memset(&msg, 0, sizeof(msg));
  328. msg.cmd = PORT_CONFIG_WRITE;
  329. msg.hss_port = port->id;
  330. msg.index = HSS_CONFIG_TX_PCR;
  331. msg.data32 = PCR_FRM_PULSE_DISABLED | PCR_MSB_ENDIAN |
  332. PCR_TX_DATA_ENABLE | PCR_SOF_NO_FBIT;
  333. if (port->clock_type == CLOCK_INT)
  334. msg.data32 |= PCR_SYNC_CLK_DIR_OUTPUT;
  335. hss_npe_send(port, &msg, "HSS_SET_TX_PCR");
  336. msg.index = HSS_CONFIG_RX_PCR;
  337. msg.data32 ^= PCR_TX_DATA_ENABLE | PCR_DCLK_EDGE_RISING;
  338. hss_npe_send(port, &msg, "HSS_SET_RX_PCR");
  339. memset(&msg, 0, sizeof(msg));
  340. msg.cmd = PORT_CONFIG_WRITE;
  341. msg.hss_port = port->id;
  342. msg.index = HSS_CONFIG_CORE_CR;
  343. msg.data32 = (port->loopback ? CCR_LOOPBACK : 0) |
  344. (port->id ? CCR_SECOND_HSS : 0);
  345. hss_npe_send(port, &msg, "HSS_SET_CORE_CR");
  346. memset(&msg, 0, sizeof(msg));
  347. msg.cmd = PORT_CONFIG_WRITE;
  348. msg.hss_port = port->id;
  349. msg.index = HSS_CONFIG_CLOCK_CR;
  350. msg.data32 = port->clock_reg;
  351. hss_npe_send(port, &msg, "HSS_SET_CLOCK_CR");
  352. memset(&msg, 0, sizeof(msg));
  353. msg.cmd = PORT_CONFIG_WRITE;
  354. msg.hss_port = port->id;
  355. msg.index = HSS_CONFIG_TX_FCR;
  356. msg.data16a = FRAME_OFFSET;
  357. msg.data16b = FRAME_SIZE - 1;
  358. hss_npe_send(port, &msg, "HSS_SET_TX_FCR");
  359. memset(&msg, 0, sizeof(msg));
  360. msg.cmd = PORT_CONFIG_WRITE;
  361. msg.hss_port = port->id;
  362. msg.index = HSS_CONFIG_RX_FCR;
  363. msg.data16a = FRAME_OFFSET;
  364. msg.data16b = FRAME_SIZE - 1;
  365. hss_npe_send(port, &msg, "HSS_SET_RX_FCR");
  366. hss_config_set_lut(port);
  367. memset(&msg, 0, sizeof(msg));
  368. msg.cmd = PORT_CONFIG_LOAD;
  369. msg.hss_port = port->id;
  370. hss_npe_send(port, &msg, "HSS_LOAD_CONFIG");
  371. if (npe_recv_message(port->npe, &msg, "HSS_LOAD_CONFIG") ||
  372. /* HSS_LOAD_CONFIG for port #1 returns port_id = #4 */
  373. msg.cmd != PORT_CONFIG_LOAD || msg.data32) {
  374. printk(KERN_CRIT "HSS-%i: HSS_LOAD_CONFIG failed\n",
  375. port->id);
  376. BUG();
  377. }
  378. /* HDLC may stop working without this - check FIXME */
  379. npe_recv_message(port->npe, &msg, "FLUSH_IT");
  380. }
  381. static void hss_set_hdlc_cfg(struct port *port)
  382. {
  383. struct msg msg;
  384. memset(&msg, 0, sizeof(msg));
  385. msg.cmd = PKT_PIPE_HDLC_CFG_WRITE;
  386. msg.hss_port = port->id;
  387. msg.data8a = port->hdlc_cfg; /* rx_cfg */
  388. msg.data8b = port->hdlc_cfg | (PKT_EXTRA_FLAGS << 3); /* tx_cfg */
  389. hss_npe_send(port, &msg, "HSS_SET_HDLC_CFG");
  390. }
  391. static u32 hss_get_status(struct port *port)
  392. {
  393. struct msg msg;
  394. memset(&msg, 0, sizeof(msg));
  395. msg.cmd = PORT_ERROR_READ;
  396. msg.hss_port = port->id;
  397. hss_npe_send(port, &msg, "PORT_ERROR_READ");
  398. if (npe_recv_message(port->npe, &msg, "PORT_ERROR_READ")) {
  399. printk(KERN_CRIT "HSS-%i: unable to read HSS status\n",
  400. port->id);
  401. BUG();
  402. }
  403. return msg.data32;
  404. }
  405. static void hss_start_hdlc(struct port *port)
  406. {
  407. struct msg msg;
  408. memset(&msg, 0, sizeof(msg));
  409. msg.cmd = PKT_PIPE_FLOW_ENABLE;
  410. msg.hss_port = port->id;
  411. msg.data32 = 0;
  412. hss_npe_send(port, &msg, "HSS_ENABLE_PKT_PIPE");
  413. }
  414. static void hss_stop_hdlc(struct port *port)
  415. {
  416. struct msg msg;
  417. memset(&msg, 0, sizeof(msg));
  418. msg.cmd = PKT_PIPE_FLOW_DISABLE;
  419. msg.hss_port = port->id;
  420. hss_npe_send(port, &msg, "HSS_DISABLE_PKT_PIPE");
  421. hss_get_status(port); /* make sure it's halted */
  422. }
  423. static int hss_load_firmware(struct port *port)
  424. {
  425. struct msg msg;
  426. int err;
  427. if (port->initialized)
  428. return 0;
  429. if (!npe_running(port->npe) &&
  430. (err = npe_load_firmware(port->npe, npe_name(port->npe),
  431. port->dev)))
  432. return err;
  433. /* HDLC mode configuration */
  434. memset(&msg, 0, sizeof(msg));
  435. msg.cmd = PKT_NUM_PIPES_WRITE;
  436. msg.hss_port = port->id;
  437. msg.data8a = PKT_NUM_PIPES;
  438. hss_npe_send(port, &msg, "HSS_SET_PKT_PIPES");
  439. msg.cmd = PKT_PIPE_FIFO_SIZEW_WRITE;
  440. msg.data8a = PKT_PIPE_FIFO_SIZEW;
  441. hss_npe_send(port, &msg, "HSS_SET_PKT_FIFO");
  442. msg.cmd = PKT_PIPE_MODE_WRITE;
  443. msg.data8a = NPE_PKT_MODE_HDLC;
  444. /* msg.data8b = inv_mask */
  445. /* msg.data8c = or_mask */
  446. hss_npe_send(port, &msg, "HSS_SET_PKT_MODE");
  447. msg.cmd = PKT_PIPE_RX_SIZE_WRITE;
  448. msg.data16a = HDLC_MAX_MRU; /* including CRC */
  449. hss_npe_send(port, &msg, "HSS_SET_PKT_RX_SIZE");
  450. msg.cmd = PKT_PIPE_IDLE_PATTERN_WRITE;
  451. msg.data32 = 0x7F7F7F7F; /* ??? FIXME */
  452. hss_npe_send(port, &msg, "HSS_SET_PKT_IDLE");
  453. port->initialized = 1;
  454. return 0;
  455. }
  456. /*****************************************************************************
  457. * packetized (HDLC) operation
  458. ****************************************************************************/
  459. static inline void debug_pkt(struct net_device *dev, const char *func,
  460. u8 *data, int len)
  461. {
  462. #if DEBUG_PKT_BYTES
  463. int i;
  464. printk(KERN_DEBUG "%s: %s(%i)", dev->name, func, len);
  465. for (i = 0; i < len; i++) {
  466. if (i >= DEBUG_PKT_BYTES)
  467. break;
  468. printk("%s%02X", !(i % 4) ? " " : "", data[i]);
  469. }
  470. printk("\n");
  471. #endif
  472. }
  473. static inline void debug_desc(u32 phys, struct desc *desc)
  474. {
  475. #if DEBUG_DESC
  476. printk(KERN_DEBUG "%X: %X %3X %3X %08X %X %X\n",
  477. phys, desc->next, desc->buf_len, desc->pkt_len,
  478. desc->data, desc->status, desc->error_count);
  479. #endif
  480. }
  481. static inline int queue_get_desc(unsigned int queue, struct port *port,
  482. int is_tx)
  483. {
  484. u32 phys, tab_phys, n_desc;
  485. struct desc *tab;
  486. if (!(phys = qmgr_get_entry(queue)))
  487. return -1;
  488. BUG_ON(phys & 0x1F);
  489. tab_phys = is_tx ? tx_desc_phys(port, 0) : rx_desc_phys(port, 0);
  490. tab = is_tx ? tx_desc_ptr(port, 0) : rx_desc_ptr(port, 0);
  491. n_desc = (phys - tab_phys) / sizeof(struct desc);
  492. BUG_ON(n_desc >= (is_tx ? TX_DESCS : RX_DESCS));
  493. debug_desc(phys, &tab[n_desc]);
  494. BUG_ON(tab[n_desc].next);
  495. return n_desc;
  496. }
  497. static inline void queue_put_desc(unsigned int queue, u32 phys,
  498. struct desc *desc)
  499. {
  500. debug_desc(phys, desc);
  501. BUG_ON(phys & 0x1F);
  502. qmgr_put_entry(queue, phys);
  503. /* Don't check for queue overflow here, we've allocated sufficient
  504. length and queues >= 32 don't support this check anyway. */
  505. }
  506. static inline void dma_unmap_tx(struct port *port, struct desc *desc)
  507. {
  508. #ifdef __ARMEB__
  509. dma_unmap_single(&port->netdev->dev, desc->data,
  510. desc->buf_len, DMA_TO_DEVICE);
  511. #else
  512. dma_unmap_single(&port->netdev->dev, desc->data & ~3,
  513. ALIGN((desc->data & 3) + desc->buf_len, 4),
  514. DMA_TO_DEVICE);
  515. #endif
  516. }
  517. static void hss_hdlc_set_carrier(void *pdev, int carrier)
  518. {
  519. struct net_device *netdev = pdev;
  520. struct port *port = dev_to_port(netdev);
  521. unsigned long flags;
  522. spin_lock_irqsave(&npe_lock, flags);
  523. port->carrier = carrier;
  524. if (!port->loopback) {
  525. if (carrier)
  526. netif_carrier_on(netdev);
  527. else
  528. netif_carrier_off(netdev);
  529. }
  530. spin_unlock_irqrestore(&npe_lock, flags);
  531. }
  532. static void hss_hdlc_rx_irq(void *pdev)
  533. {
  534. struct net_device *dev = pdev;
  535. struct port *port = dev_to_port(dev);
  536. #if DEBUG_RX
  537. printk(KERN_DEBUG "%s: hss_hdlc_rx_irq\n", dev->name);
  538. #endif
  539. qmgr_disable_irq(queue_ids[port->id].rx);
  540. napi_schedule(&port->napi);
  541. }
  542. static int hss_hdlc_poll(struct napi_struct *napi, int budget)
  543. {
  544. struct port *port = container_of(napi, struct port, napi);
  545. struct net_device *dev = port->netdev;
  546. unsigned int rxq = queue_ids[port->id].rx;
  547. unsigned int rxfreeq = queue_ids[port->id].rxfree;
  548. int received = 0;
  549. #if DEBUG_RX
  550. printk(KERN_DEBUG "%s: hss_hdlc_poll\n", dev->name);
  551. #endif
  552. while (received < budget) {
  553. struct sk_buff *skb;
  554. struct desc *desc;
  555. int n;
  556. #ifdef __ARMEB__
  557. struct sk_buff *temp;
  558. u32 phys;
  559. #endif
  560. if ((n = queue_get_desc(rxq, port, 0)) < 0) {
  561. #if DEBUG_RX
  562. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  563. " napi_complete\n", dev->name);
  564. #endif
  565. napi_complete(napi);
  566. qmgr_enable_irq(rxq);
  567. if (!qmgr_stat_empty(rxq) &&
  568. napi_reschedule(napi)) {
  569. #if DEBUG_RX
  570. printk(KERN_DEBUG "%s: hss_hdlc_poll"
  571. " napi_reschedule succeeded\n",
  572. dev->name);
  573. #endif
  574. qmgr_disable_irq(rxq);
  575. continue;
  576. }
  577. #if DEBUG_RX
  578. printk(KERN_DEBUG "%s: hss_hdlc_poll all done\n",
  579. dev->name);
  580. #endif
  581. return received; /* all work done */
  582. }
  583. desc = rx_desc_ptr(port, n);
  584. #if 0 /* FIXME - error_count counts modulo 256, perhaps we should use it */
  585. if (desc->error_count)
  586. printk(KERN_DEBUG "%s: hss_hdlc_poll status 0x%02X"
  587. " errors %u\n", dev->name, desc->status,
  588. desc->error_count);
  589. #endif
  590. skb = NULL;
  591. switch (desc->status) {
  592. case 0:
  593. #ifdef __ARMEB__
  594. if ((skb = netdev_alloc_skb(dev, RX_SIZE)) != NULL) {
  595. phys = dma_map_single(&dev->dev, skb->data,
  596. RX_SIZE,
  597. DMA_FROM_DEVICE);
  598. if (dma_mapping_error(&dev->dev, phys)) {
  599. dev_kfree_skb(skb);
  600. skb = NULL;
  601. }
  602. }
  603. #else
  604. skb = netdev_alloc_skb(dev, desc->pkt_len);
  605. #endif
  606. if (!skb)
  607. dev->stats.rx_dropped++;
  608. break;
  609. case ERR_HDLC_ALIGN:
  610. case ERR_HDLC_ABORT:
  611. dev->stats.rx_frame_errors++;
  612. dev->stats.rx_errors++;
  613. break;
  614. case ERR_HDLC_FCS:
  615. dev->stats.rx_crc_errors++;
  616. dev->stats.rx_errors++;
  617. break;
  618. case ERR_HDLC_TOO_LONG:
  619. dev->stats.rx_length_errors++;
  620. dev->stats.rx_errors++;
  621. break;
  622. default: /* FIXME - remove printk */
  623. printk(KERN_ERR "%s: hss_hdlc_poll: status 0x%02X"
  624. " errors %u\n", dev->name, desc->status,
  625. desc->error_count);
  626. dev->stats.rx_errors++;
  627. }
  628. if (!skb) {
  629. /* put the desc back on RX-ready queue */
  630. desc->buf_len = RX_SIZE;
  631. desc->pkt_len = desc->status = 0;
  632. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  633. continue;
  634. }
  635. /* process received frame */
  636. #ifdef __ARMEB__
  637. temp = skb;
  638. skb = port->rx_buff_tab[n];
  639. dma_unmap_single(&dev->dev, desc->data,
  640. RX_SIZE, DMA_FROM_DEVICE);
  641. #else
  642. dma_sync_single_for_cpu(&dev->dev, desc->data,
  643. RX_SIZE, DMA_FROM_DEVICE);
  644. memcpy_swab32((u32 *)skb->data, (u32 *)port->rx_buff_tab[n],
  645. ALIGN(desc->pkt_len, 4) / 4);
  646. #endif
  647. skb_put(skb, desc->pkt_len);
  648. debug_pkt(dev, "hss_hdlc_poll", skb->data, skb->len);
  649. skb->protocol = hdlc_type_trans(skb, dev);
  650. dev->stats.rx_packets++;
  651. dev->stats.rx_bytes += skb->len;
  652. netif_receive_skb(skb);
  653. /* put the new buffer on RX-free queue */
  654. #ifdef __ARMEB__
  655. port->rx_buff_tab[n] = temp;
  656. desc->data = phys;
  657. #endif
  658. desc->buf_len = RX_SIZE;
  659. desc->pkt_len = 0;
  660. queue_put_desc(rxfreeq, rx_desc_phys(port, n), desc);
  661. received++;
  662. }
  663. #if DEBUG_RX
  664. printk(KERN_DEBUG "hss_hdlc_poll: end, not all work done\n");
  665. #endif
  666. return received; /* not all work done */
  667. }
  668. static void hss_hdlc_txdone_irq(void *pdev)
  669. {
  670. struct net_device *dev = pdev;
  671. struct port *port = dev_to_port(dev);
  672. int n_desc;
  673. #if DEBUG_TX
  674. printk(KERN_DEBUG DRV_NAME ": hss_hdlc_txdone_irq\n");
  675. #endif
  676. while ((n_desc = queue_get_desc(queue_ids[port->id].txdone,
  677. port, 1)) >= 0) {
  678. struct desc *desc;
  679. int start;
  680. desc = tx_desc_ptr(port, n_desc);
  681. dev->stats.tx_packets++;
  682. dev->stats.tx_bytes += desc->pkt_len;
  683. dma_unmap_tx(port, desc);
  684. #if DEBUG_TX
  685. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq free %p\n",
  686. dev->name, port->tx_buff_tab[n_desc]);
  687. #endif
  688. free_buffer_irq(port->tx_buff_tab[n_desc]);
  689. port->tx_buff_tab[n_desc] = NULL;
  690. start = qmgr_stat_below_low_watermark(port->plat->txreadyq);
  691. queue_put_desc(port->plat->txreadyq,
  692. tx_desc_phys(port, n_desc), desc);
  693. if (start) { /* TX-ready queue was empty */
  694. #if DEBUG_TX
  695. printk(KERN_DEBUG "%s: hss_hdlc_txdone_irq xmit"
  696. " ready\n", dev->name);
  697. #endif
  698. netif_wake_queue(dev);
  699. }
  700. }
  701. }
  702. static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
  703. {
  704. struct port *port = dev_to_port(dev);
  705. unsigned int txreadyq = port->plat->txreadyq;
  706. int len, offset, bytes, n;
  707. void *mem;
  708. u32 phys;
  709. struct desc *desc;
  710. #if DEBUG_TX
  711. printk(KERN_DEBUG "%s: hss_hdlc_xmit\n", dev->name);
  712. #endif
  713. if (unlikely(skb->len > HDLC_MAX_MRU)) {
  714. dev_kfree_skb(skb);
  715. dev->stats.tx_errors++;
  716. return NETDEV_TX_OK;
  717. }
  718. debug_pkt(dev, "hss_hdlc_xmit", skb->data, skb->len);
  719. len = skb->len;
  720. #ifdef __ARMEB__
  721. offset = 0; /* no need to keep alignment */
  722. bytes = len;
  723. mem = skb->data;
  724. #else
  725. offset = (int)skb->data & 3; /* keep 32-bit alignment */
  726. bytes = ALIGN(offset + len, 4);
  727. if (!(mem = kmalloc(bytes, GFP_ATOMIC))) {
  728. dev_kfree_skb(skb);
  729. dev->stats.tx_dropped++;
  730. return NETDEV_TX_OK;
  731. }
  732. memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
  733. dev_kfree_skb(skb);
  734. #endif
  735. phys = dma_map_single(&dev->dev, mem, bytes, DMA_TO_DEVICE);
  736. if (dma_mapping_error(&dev->dev, phys)) {
  737. #ifdef __ARMEB__
  738. dev_kfree_skb(skb);
  739. #else
  740. kfree(mem);
  741. #endif
  742. dev->stats.tx_dropped++;
  743. return NETDEV_TX_OK;
  744. }
  745. n = queue_get_desc(txreadyq, port, 1);
  746. BUG_ON(n < 0);
  747. desc = tx_desc_ptr(port, n);
  748. #ifdef __ARMEB__
  749. port->tx_buff_tab[n] = skb;
  750. #else
  751. port->tx_buff_tab[n] = mem;
  752. #endif
  753. desc->data = phys + offset;
  754. desc->buf_len = desc->pkt_len = len;
  755. wmb();
  756. queue_put_desc(queue_ids[port->id].tx, tx_desc_phys(port, n), desc);
  757. if (qmgr_stat_below_low_watermark(txreadyq)) { /* empty */
  758. #if DEBUG_TX
  759. printk(KERN_DEBUG "%s: hss_hdlc_xmit queue full\n", dev->name);
  760. #endif
  761. netif_stop_queue(dev);
  762. /* we could miss TX ready interrupt */
  763. if (!qmgr_stat_below_low_watermark(txreadyq)) {
  764. #if DEBUG_TX
  765. printk(KERN_DEBUG "%s: hss_hdlc_xmit ready again\n",
  766. dev->name);
  767. #endif
  768. netif_wake_queue(dev);
  769. }
  770. }
  771. #if DEBUG_TX
  772. printk(KERN_DEBUG "%s: hss_hdlc_xmit end\n", dev->name);
  773. #endif
  774. return NETDEV_TX_OK;
  775. }
  776. static int request_hdlc_queues(struct port *port)
  777. {
  778. int err;
  779. err = qmgr_request_queue(queue_ids[port->id].rxfree, RX_DESCS, 0, 0,
  780. "%s:RX-free", port->netdev->name);
  781. if (err)
  782. return err;
  783. err = qmgr_request_queue(queue_ids[port->id].rx, RX_DESCS, 0, 0,
  784. "%s:RX", port->netdev->name);
  785. if (err)
  786. goto rel_rxfree;
  787. err = qmgr_request_queue(queue_ids[port->id].tx, TX_DESCS, 0, 0,
  788. "%s:TX", port->netdev->name);
  789. if (err)
  790. goto rel_rx;
  791. err = qmgr_request_queue(port->plat->txreadyq, TX_DESCS, 0, 0,
  792. "%s:TX-ready", port->netdev->name);
  793. if (err)
  794. goto rel_tx;
  795. err = qmgr_request_queue(queue_ids[port->id].txdone, TX_DESCS, 0, 0,
  796. "%s:TX-done", port->netdev->name);
  797. if (err)
  798. goto rel_txready;
  799. return 0;
  800. rel_txready:
  801. qmgr_release_queue(port->plat->txreadyq);
  802. rel_tx:
  803. qmgr_release_queue(queue_ids[port->id].tx);
  804. rel_rx:
  805. qmgr_release_queue(queue_ids[port->id].rx);
  806. rel_rxfree:
  807. qmgr_release_queue(queue_ids[port->id].rxfree);
  808. printk(KERN_DEBUG "%s: unable to request hardware queues\n",
  809. port->netdev->name);
  810. return err;
  811. }
  812. static void release_hdlc_queues(struct port *port)
  813. {
  814. qmgr_release_queue(queue_ids[port->id].rxfree);
  815. qmgr_release_queue(queue_ids[port->id].rx);
  816. qmgr_release_queue(queue_ids[port->id].txdone);
  817. qmgr_release_queue(queue_ids[port->id].tx);
  818. qmgr_release_queue(port->plat->txreadyq);
  819. }
  820. static int init_hdlc_queues(struct port *port)
  821. {
  822. int i;
  823. if (!ports_open)
  824. if (!(dma_pool = dma_pool_create(DRV_NAME, NULL,
  825. POOL_ALLOC_SIZE, 32, 0)))
  826. return -ENOMEM;
  827. if (!(port->desc_tab = dma_pool_alloc(dma_pool, GFP_KERNEL,
  828. &port->desc_tab_phys)))
  829. return -ENOMEM;
  830. memset(port->desc_tab, 0, POOL_ALLOC_SIZE);
  831. memset(port->rx_buff_tab, 0, sizeof(port->rx_buff_tab)); /* tables */
  832. memset(port->tx_buff_tab, 0, sizeof(port->tx_buff_tab));
  833. /* Setup RX buffers */
  834. for (i = 0; i < RX_DESCS; i++) {
  835. struct desc *desc = rx_desc_ptr(port, i);
  836. buffer_t *buff;
  837. void *data;
  838. #ifdef __ARMEB__
  839. if (!(buff = netdev_alloc_skb(port->netdev, RX_SIZE)))
  840. return -ENOMEM;
  841. data = buff->data;
  842. #else
  843. if (!(buff = kmalloc(RX_SIZE, GFP_KERNEL)))
  844. return -ENOMEM;
  845. data = buff;
  846. #endif
  847. desc->buf_len = RX_SIZE;
  848. desc->data = dma_map_single(&port->netdev->dev, data,
  849. RX_SIZE, DMA_FROM_DEVICE);
  850. if (dma_mapping_error(&port->netdev->dev, desc->data)) {
  851. free_buffer(buff);
  852. return -EIO;
  853. }
  854. port->rx_buff_tab[i] = buff;
  855. }
  856. return 0;
  857. }
  858. static void destroy_hdlc_queues(struct port *port)
  859. {
  860. int i;
  861. if (port->desc_tab) {
  862. for (i = 0; i < RX_DESCS; i++) {
  863. struct desc *desc = rx_desc_ptr(port, i);
  864. buffer_t *buff = port->rx_buff_tab[i];
  865. if (buff) {
  866. dma_unmap_single(&port->netdev->dev,
  867. desc->data, RX_SIZE,
  868. DMA_FROM_DEVICE);
  869. free_buffer(buff);
  870. }
  871. }
  872. for (i = 0; i < TX_DESCS; i++) {
  873. struct desc *desc = tx_desc_ptr(port, i);
  874. buffer_t *buff = port->tx_buff_tab[i];
  875. if (buff) {
  876. dma_unmap_tx(port, desc);
  877. free_buffer(buff);
  878. }
  879. }
  880. dma_pool_free(dma_pool, port->desc_tab, port->desc_tab_phys);
  881. port->desc_tab = NULL;
  882. }
  883. if (!ports_open && dma_pool) {
  884. dma_pool_destroy(dma_pool);
  885. dma_pool = NULL;
  886. }
  887. }
  888. static int hss_hdlc_open(struct net_device *dev)
  889. {
  890. struct port *port = dev_to_port(dev);
  891. unsigned long flags;
  892. int i, err = 0;
  893. if ((err = hdlc_open(dev)))
  894. return err;
  895. if ((err = hss_load_firmware(port)))
  896. goto err_hdlc_close;
  897. if ((err = request_hdlc_queues(port)))
  898. goto err_hdlc_close;
  899. if ((err = init_hdlc_queues(port)))
  900. goto err_destroy_queues;
  901. spin_lock_irqsave(&npe_lock, flags);
  902. if (port->plat->open)
  903. if ((err = port->plat->open(port->id, dev,
  904. hss_hdlc_set_carrier)))
  905. goto err_unlock;
  906. spin_unlock_irqrestore(&npe_lock, flags);
  907. /* Populate queues with buffers, no failure after this point */
  908. for (i = 0; i < TX_DESCS; i++)
  909. queue_put_desc(port->plat->txreadyq,
  910. tx_desc_phys(port, i), tx_desc_ptr(port, i));
  911. for (i = 0; i < RX_DESCS; i++)
  912. queue_put_desc(queue_ids[port->id].rxfree,
  913. rx_desc_phys(port, i), rx_desc_ptr(port, i));
  914. napi_enable(&port->napi);
  915. netif_start_queue(dev);
  916. qmgr_set_irq(queue_ids[port->id].rx, QUEUE_IRQ_SRC_NOT_EMPTY,
  917. hss_hdlc_rx_irq, dev);
  918. qmgr_set_irq(queue_ids[port->id].txdone, QUEUE_IRQ_SRC_NOT_EMPTY,
  919. hss_hdlc_txdone_irq, dev);
  920. qmgr_enable_irq(queue_ids[port->id].txdone);
  921. ports_open++;
  922. hss_set_hdlc_cfg(port);
  923. hss_config(port);
  924. hss_start_hdlc(port);
  925. /* we may already have RX data, enables IRQ */
  926. napi_schedule(&port->napi);
  927. return 0;
  928. err_unlock:
  929. spin_unlock_irqrestore(&npe_lock, flags);
  930. err_destroy_queues:
  931. destroy_hdlc_queues(port);
  932. release_hdlc_queues(port);
  933. err_hdlc_close:
  934. hdlc_close(dev);
  935. return err;
  936. }
  937. static int hss_hdlc_close(struct net_device *dev)
  938. {
  939. struct port *port = dev_to_port(dev);
  940. unsigned long flags;
  941. int i, buffs = RX_DESCS; /* allocated RX buffers */
  942. spin_lock_irqsave(&npe_lock, flags);
  943. ports_open--;
  944. qmgr_disable_irq(queue_ids[port->id].rx);
  945. netif_stop_queue(dev);
  946. napi_disable(&port->napi);
  947. hss_stop_hdlc(port);
  948. while (queue_get_desc(queue_ids[port->id].rxfree, port, 0) >= 0)
  949. buffs--;
  950. while (queue_get_desc(queue_ids[port->id].rx, port, 0) >= 0)
  951. buffs--;
  952. if (buffs)
  953. printk(KERN_CRIT "%s: unable to drain RX queue, %i buffer(s)"
  954. " left in NPE\n", dev->name, buffs);
  955. buffs = TX_DESCS;
  956. while (queue_get_desc(queue_ids[port->id].tx, port, 1) >= 0)
  957. buffs--; /* cancel TX */
  958. i = 0;
  959. do {
  960. while (queue_get_desc(port->plat->txreadyq, port, 1) >= 0)
  961. buffs--;
  962. if (!buffs)
  963. break;
  964. } while (++i < MAX_CLOSE_WAIT);
  965. if (buffs)
  966. printk(KERN_CRIT "%s: unable to drain TX queue, %i buffer(s) "
  967. "left in NPE\n", dev->name, buffs);
  968. #if DEBUG_CLOSE
  969. if (!buffs)
  970. printk(KERN_DEBUG "Draining TX queues took %i cycles\n", i);
  971. #endif
  972. qmgr_disable_irq(queue_ids[port->id].txdone);
  973. if (port->plat->close)
  974. port->plat->close(port->id, dev);
  975. spin_unlock_irqrestore(&npe_lock, flags);
  976. destroy_hdlc_queues(port);
  977. release_hdlc_queues(port);
  978. hdlc_close(dev);
  979. return 0;
  980. }
  981. static int hss_hdlc_attach(struct net_device *dev, unsigned short encoding,
  982. unsigned short parity)
  983. {
  984. struct port *port = dev_to_port(dev);
  985. if (encoding != ENCODING_NRZ)
  986. return -EINVAL;
  987. switch(parity) {
  988. case PARITY_CRC16_PR1_CCITT:
  989. port->hdlc_cfg = 0;
  990. return 0;
  991. case PARITY_CRC32_PR1_CCITT:
  992. port->hdlc_cfg = PKT_HDLC_CRC_32;
  993. return 0;
  994. default:
  995. return -EINVAL;
  996. }
  997. }
  998. static u32 check_clock(u32 rate, u32 a, u32 b, u32 c,
  999. u32 *best, u32 *best_diff, u32 *reg)
  1000. {
  1001. /* a is 10-bit, b is 10-bit, c is 12-bit */
  1002. u64 new_rate;
  1003. u32 new_diff;
  1004. new_rate = ixp4xx_timer_freq * (u64)(c + 1);
  1005. do_div(new_rate, a * (c + 1) + b + 1);
  1006. new_diff = abs((u32)new_rate - rate);
  1007. if (new_diff < *best_diff) {
  1008. *best = new_rate;
  1009. *best_diff = new_diff;
  1010. *reg = (a << 22) | (b << 12) | c;
  1011. }
  1012. return new_diff;
  1013. }
  1014. static void find_best_clock(u32 rate, u32 *best, u32 *reg)
  1015. {
  1016. u32 a, b, diff = 0xFFFFFFFF;
  1017. a = ixp4xx_timer_freq / rate;
  1018. if (a > 0x3FF) { /* 10-bit value - we can go as slow as ca. 65 kb/s */
  1019. check_clock(rate, 0x3FF, 1, 1, best, &diff, reg);
  1020. return;
  1021. }
  1022. if (a == 0) { /* > 66.666 MHz */
  1023. a = 1; /* minimum divider is 1 (a = 0, b = 1, c = 1) */
  1024. rate = ixp4xx_timer_freq;
  1025. }
  1026. if (rate * a == ixp4xx_timer_freq) { /* don't divide by 0 later */
  1027. check_clock(rate, a - 1, 1, 1, best, &diff, reg);
  1028. return;
  1029. }
  1030. for (b = 0; b < 0x400; b++) {
  1031. u64 c = (b + 1) * (u64)rate;
  1032. do_div(c, ixp4xx_timer_freq - rate * a);
  1033. c--;
  1034. if (c >= 0xFFF) { /* 12-bit - no need to check more 'b's */
  1035. if (b == 0 && /* also try a bit higher rate */
  1036. !check_clock(rate, a - 1, 1, 1, best, &diff, reg))
  1037. return;
  1038. check_clock(rate, a, b, 0xFFF, best, &diff, reg);
  1039. return;
  1040. }
  1041. if (!check_clock(rate, a, b, c, best, &diff, reg))
  1042. return;
  1043. if (!check_clock(rate, a, b, c + 1, best, &diff, reg))
  1044. return;
  1045. }
  1046. }
  1047. static int hss_hdlc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1048. {
  1049. const size_t size = sizeof(sync_serial_settings);
  1050. sync_serial_settings new_line;
  1051. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1052. struct port *port = dev_to_port(dev);
  1053. unsigned long flags;
  1054. int clk;
  1055. if (cmd != SIOCWANDEV)
  1056. return hdlc_ioctl(dev, ifr, cmd);
  1057. switch(ifr->ifr_settings.type) {
  1058. case IF_GET_IFACE:
  1059. ifr->ifr_settings.type = IF_IFACE_V35;
  1060. if (ifr->ifr_settings.size < size) {
  1061. ifr->ifr_settings.size = size; /* data size wanted */
  1062. return -ENOBUFS;
  1063. }
  1064. memset(&new_line, 0, sizeof(new_line));
  1065. new_line.clock_type = port->clock_type;
  1066. new_line.clock_rate = port->clock_rate;
  1067. new_line.loopback = port->loopback;
  1068. if (copy_to_user(line, &new_line, size))
  1069. return -EFAULT;
  1070. return 0;
  1071. case IF_IFACE_SYNC_SERIAL:
  1072. case IF_IFACE_V35:
  1073. if(!capable(CAP_NET_ADMIN))
  1074. return -EPERM;
  1075. if (copy_from_user(&new_line, line, size))
  1076. return -EFAULT;
  1077. clk = new_line.clock_type;
  1078. if (port->plat->set_clock)
  1079. clk = port->plat->set_clock(port->id, clk);
  1080. if (clk != CLOCK_EXT && clk != CLOCK_INT)
  1081. return -EINVAL; /* No such clock setting */
  1082. if (new_line.loopback != 0 && new_line.loopback != 1)
  1083. return -EINVAL;
  1084. port->clock_type = clk; /* Update settings */
  1085. if (clk == CLOCK_INT)
  1086. find_best_clock(new_line.clock_rate, &port->clock_rate,
  1087. &port->clock_reg);
  1088. else {
  1089. port->clock_rate = 0;
  1090. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1091. }
  1092. port->loopback = new_line.loopback;
  1093. spin_lock_irqsave(&npe_lock, flags);
  1094. if (dev->flags & IFF_UP)
  1095. hss_config(port);
  1096. if (port->loopback || port->carrier)
  1097. netif_carrier_on(port->netdev);
  1098. else
  1099. netif_carrier_off(port->netdev);
  1100. spin_unlock_irqrestore(&npe_lock, flags);
  1101. return 0;
  1102. default:
  1103. return hdlc_ioctl(dev, ifr, cmd);
  1104. }
  1105. }
  1106. /*****************************************************************************
  1107. * initialization
  1108. ****************************************************************************/
  1109. static const struct net_device_ops hss_hdlc_ops = {
  1110. .ndo_open = hss_hdlc_open,
  1111. .ndo_stop = hss_hdlc_close,
  1112. .ndo_change_mtu = hdlc_change_mtu,
  1113. .ndo_start_xmit = hdlc_start_xmit,
  1114. .ndo_do_ioctl = hss_hdlc_ioctl,
  1115. };
  1116. static int __devinit hss_init_one(struct platform_device *pdev)
  1117. {
  1118. struct port *port;
  1119. struct net_device *dev;
  1120. hdlc_device *hdlc;
  1121. int err;
  1122. if ((port = kzalloc(sizeof(*port), GFP_KERNEL)) == NULL)
  1123. return -ENOMEM;
  1124. if ((port->npe = npe_request(0)) == NULL) {
  1125. err = -ENODEV;
  1126. goto err_free;
  1127. }
  1128. if ((port->netdev = dev = alloc_hdlcdev(port)) == NULL) {
  1129. err = -ENOMEM;
  1130. goto err_plat;
  1131. }
  1132. SET_NETDEV_DEV(dev, &pdev->dev);
  1133. hdlc = dev_to_hdlc(dev);
  1134. hdlc->attach = hss_hdlc_attach;
  1135. hdlc->xmit = hss_hdlc_xmit;
  1136. dev->netdev_ops = &hss_hdlc_ops;
  1137. dev->tx_queue_len = 100;
  1138. port->clock_type = CLOCK_EXT;
  1139. port->clock_rate = 0;
  1140. port->clock_reg = CLK42X_SPEED_2048KHZ;
  1141. port->id = pdev->id;
  1142. port->dev = &pdev->dev;
  1143. port->plat = pdev->dev.platform_data;
  1144. netif_napi_add(dev, &port->napi, hss_hdlc_poll, NAPI_WEIGHT);
  1145. if ((err = register_hdlc_device(dev)))
  1146. goto err_free_netdev;
  1147. platform_set_drvdata(pdev, port);
  1148. printk(KERN_INFO "%s: HSS-%i\n", dev->name, port->id);
  1149. return 0;
  1150. err_free_netdev:
  1151. free_netdev(dev);
  1152. err_plat:
  1153. npe_release(port->npe);
  1154. err_free:
  1155. kfree(port);
  1156. return err;
  1157. }
  1158. static int __devexit hss_remove_one(struct platform_device *pdev)
  1159. {
  1160. struct port *port = platform_get_drvdata(pdev);
  1161. unregister_hdlc_device(port->netdev);
  1162. free_netdev(port->netdev);
  1163. npe_release(port->npe);
  1164. platform_set_drvdata(pdev, NULL);
  1165. kfree(port);
  1166. return 0;
  1167. }
  1168. static struct platform_driver ixp4xx_hss_driver = {
  1169. .driver.name = DRV_NAME,
  1170. .probe = hss_init_one,
  1171. .remove = hss_remove_one,
  1172. };
  1173. static int __init hss_init_module(void)
  1174. {
  1175. if ((ixp4xx_read_feature_bits() &
  1176. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS)) !=
  1177. (IXP4XX_FEATURE_HDLC | IXP4XX_FEATURE_HSS))
  1178. return -ENODEV;
  1179. spin_lock_init(&npe_lock);
  1180. return platform_driver_register(&ixp4xx_hss_driver);
  1181. }
  1182. static void __exit hss_cleanup_module(void)
  1183. {
  1184. platform_driver_unregister(&ixp4xx_hss_driver);
  1185. }
  1186. MODULE_AUTHOR("Krzysztof Halasa");
  1187. MODULE_DESCRIPTION("Intel IXP4xx HSS driver");
  1188. MODULE_LICENSE("GPL v2");
  1189. MODULE_ALIAS("platform:ixp4xx_hss");
  1190. module_init(hss_init_module);
  1191. module_exit(hss_cleanup_module);