tlclk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Telecom Clock driver for Intel NetStructure(tm) MPCBL0010
  3. *
  4. * Copyright (C) 2005 Kontron Canada
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <sebastien.bouchard@ca.kontron.com> and the current
  24. * Maintainer <mark.gross@intel.com>
  25. *
  26. * Description : This is the TELECOM CLOCK module driver for the ATCA
  27. * MPCBL0010 ATCA computer.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/kernel.h> /* printk() */
  33. #include <linux/fs.h> /* everything... */
  34. #include <linux/errno.h> /* error codes */
  35. #include <linux/slab.h>
  36. #include <linux/ioport.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/timer.h>
  40. #include <linux/sysfs.h>
  41. #include <linux/device.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/platform_device.h>
  44. #include <asm/io.h> /* inb/outb */
  45. #include <asm/uaccess.h>
  46. MODULE_AUTHOR("Sebastien Bouchard <sebastien.bouchard@ca.kontron.com>");
  47. MODULE_LICENSE("GPL");
  48. /*Hardware Reset of the PLL */
  49. #define RESET_ON 0x00
  50. #define RESET_OFF 0x01
  51. /* MODE SELECT */
  52. #define NORMAL_MODE 0x00
  53. #define HOLDOVER_MODE 0x10
  54. #define FREERUN_MODE 0x20
  55. /* FILTER SELECT */
  56. #define FILTER_6HZ 0x04
  57. #define FILTER_12HZ 0x00
  58. /* SELECT REFERENCE FREQUENCY */
  59. #define REF_CLK1_8kHz 0x00
  60. #define REF_CLK2_19_44MHz 0x02
  61. /* Select primary or secondary redundant clock */
  62. #define PRIMARY_CLOCK 0x00
  63. #define SECONDARY_CLOCK 0x01
  64. /* CLOCK TRANSMISSION DEFINE */
  65. #define CLK_8kHz 0xff
  66. #define CLK_16_384MHz 0xfb
  67. #define CLK_1_544MHz 0x00
  68. #define CLK_2_048MHz 0x01
  69. #define CLK_4_096MHz 0x02
  70. #define CLK_6_312MHz 0x03
  71. #define CLK_8_192MHz 0x04
  72. #define CLK_19_440MHz 0x06
  73. #define CLK_8_592MHz 0x08
  74. #define CLK_11_184MHz 0x09
  75. #define CLK_34_368MHz 0x0b
  76. #define CLK_44_736MHz 0x0a
  77. /* RECEIVED REFERENCE */
  78. #define AMC_B1 0
  79. #define AMC_B2 1
  80. /* HARDWARE SWITCHING DEFINE */
  81. #define HW_ENABLE 0x80
  82. #define HW_DISABLE 0x00
  83. /* HARDWARE SWITCHING MODE DEFINE */
  84. #define PLL_HOLDOVER 0x40
  85. #define LOST_CLOCK 0x00
  86. /* ALARMS DEFINE */
  87. #define UNLOCK_MASK 0x10
  88. #define HOLDOVER_MASK 0x20
  89. #define SEC_LOST_MASK 0x40
  90. #define PRI_LOST_MASK 0x80
  91. /* INTERRUPT CAUSE DEFINE */
  92. #define PRI_LOS_01_MASK 0x01
  93. #define PRI_LOS_10_MASK 0x02
  94. #define SEC_LOS_01_MASK 0x04
  95. #define SEC_LOS_10_MASK 0x08
  96. #define HOLDOVER_01_MASK 0x10
  97. #define HOLDOVER_10_MASK 0x20
  98. #define UNLOCK_01_MASK 0x40
  99. #define UNLOCK_10_MASK 0x80
  100. struct tlclk_alarms {
  101. __u32 lost_clocks;
  102. __u32 lost_primary_clock;
  103. __u32 lost_secondary_clock;
  104. __u32 primary_clock_back;
  105. __u32 secondary_clock_back;
  106. __u32 switchover_primary;
  107. __u32 switchover_secondary;
  108. __u32 pll_holdover;
  109. __u32 pll_end_holdover;
  110. __u32 pll_lost_sync;
  111. __u32 pll_sync;
  112. };
  113. /* Telecom clock I/O register definition */
  114. #define TLCLK_BASE 0xa08
  115. #define TLCLK_REG0 TLCLK_BASE
  116. #define TLCLK_REG1 (TLCLK_BASE+1)
  117. #define TLCLK_REG2 (TLCLK_BASE+2)
  118. #define TLCLK_REG3 (TLCLK_BASE+3)
  119. #define TLCLK_REG4 (TLCLK_BASE+4)
  120. #define TLCLK_REG5 (TLCLK_BASE+5)
  121. #define TLCLK_REG6 (TLCLK_BASE+6)
  122. #define TLCLK_REG7 (TLCLK_BASE+7)
  123. #define SET_PORT_BITS(port, mask, val) outb(((inb(port) & mask) | val), port)
  124. /* 0 = Dynamic allocation of the major device number */
  125. #define TLCLK_MAJOR 0
  126. /* sysfs interface definition:
  127. Upon loading the driver will create a sysfs directory under
  128. /sys/devices/platform/telco_clock.
  129. This directory exports the following interfaces. There operation is
  130. documented in the MCPBL0010 TPS under the Telecom Clock API section, 11.4.
  131. alarms :
  132. current_ref :
  133. received_ref_clk3a :
  134. received_ref_clk3b :
  135. enable_clk3a_output :
  136. enable_clk3b_output :
  137. enable_clka0_output :
  138. enable_clka1_output :
  139. enable_clkb0_output :
  140. enable_clkb1_output :
  141. filter_select :
  142. hardware_switching :
  143. hardware_switching_mode :
  144. telclock_version :
  145. mode_select :
  146. refalign :
  147. reset :
  148. select_amcb1_transmit_clock :
  149. select_amcb2_transmit_clock :
  150. select_redundant_clock :
  151. select_ref_frequency :
  152. All sysfs interfaces are integers in hex format, i.e echo 99 > refalign
  153. has the same effect as echo 0x99 > refalign.
  154. */
  155. static unsigned int telclk_interrupt;
  156. static int int_events; /* Event that generate a interrupt */
  157. static int got_event; /* if events processing have been done */
  158. static void switchover_timeout(unsigned long data);
  159. static struct timer_list switchover_timer =
  160. TIMER_INITIALIZER(switchover_timeout , 0, 0);
  161. static unsigned long tlclk_timer_data;
  162. static struct tlclk_alarms *alarm_events;
  163. static DEFINE_SPINLOCK(event_lock);
  164. static int tlclk_major = TLCLK_MAJOR;
  165. static irqreturn_t tlclk_interrupt(int irq, void *dev_id);
  166. static DECLARE_WAIT_QUEUE_HEAD(wq);
  167. static unsigned long useflags;
  168. static DEFINE_MUTEX(tlclk_mutex);
  169. static int tlclk_open(struct inode *inode, struct file *filp)
  170. {
  171. int result;
  172. if (test_and_set_bit(0, &useflags))
  173. return -EBUSY;
  174. /* this legacy device is always one per system and it doesn't
  175. * know how to handle multiple concurrent clients.
  176. */
  177. /* Make sure there is no interrupt pending while
  178. * initialising interrupt handler */
  179. inb(TLCLK_REG6);
  180. /* This device is wired through the FPGA IO space of the ATCA blade
  181. * we can't share this IRQ */
  182. result = request_irq(telclk_interrupt, &tlclk_interrupt,
  183. IRQF_DISABLED, "telco_clock", tlclk_interrupt);
  184. if (result == -EBUSY) {
  185. printk(KERN_ERR "tlclk: Interrupt can't be reserved.\n");
  186. return -EBUSY;
  187. }
  188. inb(TLCLK_REG6); /* Clear interrupt events */
  189. return 0;
  190. }
  191. static int tlclk_release(struct inode *inode, struct file *filp)
  192. {
  193. free_irq(telclk_interrupt, tlclk_interrupt);
  194. clear_bit(0, &useflags);
  195. return 0;
  196. }
  197. static ssize_t tlclk_read(struct file *filp, char __user *buf, size_t count,
  198. loff_t *f_pos)
  199. {
  200. if (count < sizeof(struct tlclk_alarms))
  201. return -EIO;
  202. if (mutex_lock_interruptible(&tlclk_mutex))
  203. return -EINTR;
  204. wait_event_interruptible(wq, got_event);
  205. if (copy_to_user(buf, alarm_events, sizeof(struct tlclk_alarms))) {
  206. mutex_unlock(&tlclk_mutex);
  207. return -EFAULT;
  208. }
  209. memset(alarm_events, 0, sizeof(struct tlclk_alarms));
  210. got_event = 0;
  211. mutex_unlock(&tlclk_mutex);
  212. return sizeof(struct tlclk_alarms);
  213. }
  214. static const struct file_operations tlclk_fops = {
  215. .read = tlclk_read,
  216. .open = tlclk_open,
  217. .release = tlclk_release,
  218. };
  219. static struct miscdevice tlclk_miscdev = {
  220. .minor = MISC_DYNAMIC_MINOR,
  221. .name = "telco_clock",
  222. .fops = &tlclk_fops,
  223. };
  224. static ssize_t show_current_ref(struct device *d,
  225. struct device_attribute *attr, char *buf)
  226. {
  227. unsigned long ret_val;
  228. unsigned long flags;
  229. spin_lock_irqsave(&event_lock, flags);
  230. ret_val = ((inb(TLCLK_REG1) & 0x08) >> 3);
  231. spin_unlock_irqrestore(&event_lock, flags);
  232. return sprintf(buf, "0x%lX\n", ret_val);
  233. }
  234. static DEVICE_ATTR(current_ref, S_IRUGO, show_current_ref, NULL);
  235. static ssize_t show_telclock_version(struct device *d,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. unsigned long ret_val;
  239. unsigned long flags;
  240. spin_lock_irqsave(&event_lock, flags);
  241. ret_val = inb(TLCLK_REG5);
  242. spin_unlock_irqrestore(&event_lock, flags);
  243. return sprintf(buf, "0x%lX\n", ret_val);
  244. }
  245. static DEVICE_ATTR(telclock_version, S_IRUGO,
  246. show_telclock_version, NULL);
  247. static ssize_t show_alarms(struct device *d,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. unsigned long ret_val;
  251. unsigned long flags;
  252. spin_lock_irqsave(&event_lock, flags);
  253. ret_val = (inb(TLCLK_REG2) & 0xf0);
  254. spin_unlock_irqrestore(&event_lock, flags);
  255. return sprintf(buf, "0x%lX\n", ret_val);
  256. }
  257. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  258. static ssize_t store_received_ref_clk3a(struct device *d,
  259. struct device_attribute *attr, const char *buf, size_t count)
  260. {
  261. unsigned long tmp;
  262. unsigned char val;
  263. unsigned long flags;
  264. sscanf(buf, "%lX", &tmp);
  265. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  266. val = (unsigned char)tmp;
  267. spin_lock_irqsave(&event_lock, flags);
  268. SET_PORT_BITS(TLCLK_REG1, 0xef, val);
  269. spin_unlock_irqrestore(&event_lock, flags);
  270. return strnlen(buf, count);
  271. }
  272. static DEVICE_ATTR(received_ref_clk3a, (S_IWUSR|S_IWGRP), NULL,
  273. store_received_ref_clk3a);
  274. static ssize_t store_received_ref_clk3b(struct device *d,
  275. struct device_attribute *attr, const char *buf, size_t count)
  276. {
  277. unsigned long tmp;
  278. unsigned char val;
  279. unsigned long flags;
  280. sscanf(buf, "%lX", &tmp);
  281. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  282. val = (unsigned char)tmp;
  283. spin_lock_irqsave(&event_lock, flags);
  284. SET_PORT_BITS(TLCLK_REG1, 0xdf, val << 1);
  285. spin_unlock_irqrestore(&event_lock, flags);
  286. return strnlen(buf, count);
  287. }
  288. static DEVICE_ATTR(received_ref_clk3b, (S_IWUSR|S_IWGRP), NULL,
  289. store_received_ref_clk3b);
  290. static ssize_t store_enable_clk3b_output(struct device *d,
  291. struct device_attribute *attr, const char *buf, size_t count)
  292. {
  293. unsigned long tmp;
  294. unsigned char val;
  295. unsigned long flags;
  296. sscanf(buf, "%lX", &tmp);
  297. dev_dbg(d, ": tmp = 0x%lX\n", tmp);
  298. val = (unsigned char)tmp;
  299. spin_lock_irqsave(&event_lock, flags);
  300. SET_PORT_BITS(TLCLK_REG3, 0x7f, val << 7);
  301. spin_unlock_irqrestore(&event_lock, flags);
  302. return strnlen(buf, count);
  303. }
  304. static DEVICE_ATTR(enable_clk3b_output, (S_IWUSR|S_IWGRP), NULL,
  305. store_enable_clk3b_output);
  306. static ssize_t store_enable_clk3a_output(struct device *d,
  307. struct device_attribute *attr, const char *buf, size_t count)
  308. {
  309. unsigned long flags;
  310. unsigned long tmp;
  311. unsigned char val;
  312. sscanf(buf, "%lX", &tmp);
  313. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  314. val = (unsigned char)tmp;
  315. spin_lock_irqsave(&event_lock, flags);
  316. SET_PORT_BITS(TLCLK_REG3, 0xbf, val << 6);
  317. spin_unlock_irqrestore(&event_lock, flags);
  318. return strnlen(buf, count);
  319. }
  320. static DEVICE_ATTR(enable_clk3a_output, (S_IWUSR|S_IWGRP), NULL,
  321. store_enable_clk3a_output);
  322. static ssize_t store_enable_clkb1_output(struct device *d,
  323. struct device_attribute *attr, const char *buf, size_t count)
  324. {
  325. unsigned long flags;
  326. unsigned long tmp;
  327. unsigned char val;
  328. sscanf(buf, "%lX", &tmp);
  329. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  330. val = (unsigned char)tmp;
  331. spin_lock_irqsave(&event_lock, flags);
  332. SET_PORT_BITS(TLCLK_REG2, 0xf7, val << 3);
  333. spin_unlock_irqrestore(&event_lock, flags);
  334. return strnlen(buf, count);
  335. }
  336. static DEVICE_ATTR(enable_clkb1_output, (S_IWUSR|S_IWGRP), NULL,
  337. store_enable_clkb1_output);
  338. static ssize_t store_enable_clka1_output(struct device *d,
  339. struct device_attribute *attr, const char *buf, size_t count)
  340. {
  341. unsigned long flags;
  342. unsigned long tmp;
  343. unsigned char val;
  344. sscanf(buf, "%lX", &tmp);
  345. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  346. val = (unsigned char)tmp;
  347. spin_lock_irqsave(&event_lock, flags);
  348. SET_PORT_BITS(TLCLK_REG2, 0xfb, val << 2);
  349. spin_unlock_irqrestore(&event_lock, flags);
  350. return strnlen(buf, count);
  351. }
  352. static DEVICE_ATTR(enable_clka1_output, (S_IWUSR|S_IWGRP), NULL,
  353. store_enable_clka1_output);
  354. static ssize_t store_enable_clkb0_output(struct device *d,
  355. struct device_attribute *attr, const char *buf, size_t count)
  356. {
  357. unsigned long flags;
  358. unsigned long tmp;
  359. unsigned char val;
  360. sscanf(buf, "%lX", &tmp);
  361. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  362. val = (unsigned char)tmp;
  363. spin_lock_irqsave(&event_lock, flags);
  364. SET_PORT_BITS(TLCLK_REG2, 0xfd, val << 1);
  365. spin_unlock_irqrestore(&event_lock, flags);
  366. return strnlen(buf, count);
  367. }
  368. static DEVICE_ATTR(enable_clkb0_output, (S_IWUSR|S_IWGRP), NULL,
  369. store_enable_clkb0_output);
  370. static ssize_t store_enable_clka0_output(struct device *d,
  371. struct device_attribute *attr, const char *buf, size_t count)
  372. {
  373. unsigned long flags;
  374. unsigned long tmp;
  375. unsigned char val;
  376. sscanf(buf, "%lX", &tmp);
  377. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  378. val = (unsigned char)tmp;
  379. spin_lock_irqsave(&event_lock, flags);
  380. SET_PORT_BITS(TLCLK_REG2, 0xfe, val);
  381. spin_unlock_irqrestore(&event_lock, flags);
  382. return strnlen(buf, count);
  383. }
  384. static DEVICE_ATTR(enable_clka0_output, (S_IWUSR|S_IWGRP), NULL,
  385. store_enable_clka0_output);
  386. static ssize_t store_select_amcb2_transmit_clock(struct device *d,
  387. struct device_attribute *attr, const char *buf, size_t count)
  388. {
  389. unsigned long flags;
  390. unsigned long tmp;
  391. unsigned char val;
  392. sscanf(buf, "%lX", &tmp);
  393. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  394. val = (unsigned char)tmp;
  395. spin_lock_irqsave(&event_lock, flags);
  396. if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
  397. SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x28);
  398. SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
  399. } else if (val >= CLK_8_592MHz) {
  400. SET_PORT_BITS(TLCLK_REG3, 0xc7, 0x38);
  401. switch (val) {
  402. case CLK_8_592MHz:
  403. SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
  404. break;
  405. case CLK_11_184MHz:
  406. SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
  407. break;
  408. case CLK_34_368MHz:
  409. SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
  410. break;
  411. case CLK_44_736MHz:
  412. SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
  413. break;
  414. }
  415. } else
  416. SET_PORT_BITS(TLCLK_REG3, 0xc7, val << 3);
  417. spin_unlock_irqrestore(&event_lock, flags);
  418. return strnlen(buf, count);
  419. }
  420. static DEVICE_ATTR(select_amcb2_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
  421. store_select_amcb2_transmit_clock);
  422. static ssize_t store_select_amcb1_transmit_clock(struct device *d,
  423. struct device_attribute *attr, const char *buf, size_t count)
  424. {
  425. unsigned long tmp;
  426. unsigned char val;
  427. unsigned long flags;
  428. sscanf(buf, "%lX", &tmp);
  429. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  430. val = (unsigned char)tmp;
  431. spin_lock_irqsave(&event_lock, flags);
  432. if ((val == CLK_8kHz) || (val == CLK_16_384MHz)) {
  433. SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x5);
  434. SET_PORT_BITS(TLCLK_REG1, 0xfb, ~val);
  435. } else if (val >= CLK_8_592MHz) {
  436. SET_PORT_BITS(TLCLK_REG3, 0xf8, 0x7);
  437. switch (val) {
  438. case CLK_8_592MHz:
  439. SET_PORT_BITS(TLCLK_REG0, 0xfc, 2);
  440. break;
  441. case CLK_11_184MHz:
  442. SET_PORT_BITS(TLCLK_REG0, 0xfc, 0);
  443. break;
  444. case CLK_34_368MHz:
  445. SET_PORT_BITS(TLCLK_REG0, 0xfc, 3);
  446. break;
  447. case CLK_44_736MHz:
  448. SET_PORT_BITS(TLCLK_REG0, 0xfc, 1);
  449. break;
  450. }
  451. } else
  452. SET_PORT_BITS(TLCLK_REG3, 0xf8, val);
  453. spin_unlock_irqrestore(&event_lock, flags);
  454. return strnlen(buf, count);
  455. }
  456. static DEVICE_ATTR(select_amcb1_transmit_clock, (S_IWUSR|S_IWGRP), NULL,
  457. store_select_amcb1_transmit_clock);
  458. static ssize_t store_select_redundant_clock(struct device *d,
  459. struct device_attribute *attr, const char *buf, size_t count)
  460. {
  461. unsigned long tmp;
  462. unsigned char val;
  463. unsigned long flags;
  464. sscanf(buf, "%lX", &tmp);
  465. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  466. val = (unsigned char)tmp;
  467. spin_lock_irqsave(&event_lock, flags);
  468. SET_PORT_BITS(TLCLK_REG1, 0xfe, val);
  469. spin_unlock_irqrestore(&event_lock, flags);
  470. return strnlen(buf, count);
  471. }
  472. static DEVICE_ATTR(select_redundant_clock, (S_IWUSR|S_IWGRP), NULL,
  473. store_select_redundant_clock);
  474. static ssize_t store_select_ref_frequency(struct device *d,
  475. struct device_attribute *attr, const char *buf, size_t count)
  476. {
  477. unsigned long tmp;
  478. unsigned char val;
  479. unsigned long flags;
  480. sscanf(buf, "%lX", &tmp);
  481. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  482. val = (unsigned char)tmp;
  483. spin_lock_irqsave(&event_lock, flags);
  484. SET_PORT_BITS(TLCLK_REG1, 0xfd, val);
  485. spin_unlock_irqrestore(&event_lock, flags);
  486. return strnlen(buf, count);
  487. }
  488. static DEVICE_ATTR(select_ref_frequency, (S_IWUSR|S_IWGRP), NULL,
  489. store_select_ref_frequency);
  490. static ssize_t store_filter_select(struct device *d,
  491. struct device_attribute *attr, const char *buf, size_t count)
  492. {
  493. unsigned long tmp;
  494. unsigned char val;
  495. unsigned long flags;
  496. sscanf(buf, "%lX", &tmp);
  497. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  498. val = (unsigned char)tmp;
  499. spin_lock_irqsave(&event_lock, flags);
  500. SET_PORT_BITS(TLCLK_REG0, 0xfb, val);
  501. spin_unlock_irqrestore(&event_lock, flags);
  502. return strnlen(buf, count);
  503. }
  504. static DEVICE_ATTR(filter_select, (S_IWUSR|S_IWGRP), NULL, store_filter_select);
  505. static ssize_t store_hardware_switching_mode(struct device *d,
  506. struct device_attribute *attr, const char *buf, size_t count)
  507. {
  508. unsigned long tmp;
  509. unsigned char val;
  510. unsigned long flags;
  511. sscanf(buf, "%lX", &tmp);
  512. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  513. val = (unsigned char)tmp;
  514. spin_lock_irqsave(&event_lock, flags);
  515. SET_PORT_BITS(TLCLK_REG0, 0xbf, val);
  516. spin_unlock_irqrestore(&event_lock, flags);
  517. return strnlen(buf, count);
  518. }
  519. static DEVICE_ATTR(hardware_switching_mode, (S_IWUSR|S_IWGRP), NULL,
  520. store_hardware_switching_mode);
  521. static ssize_t store_hardware_switching(struct device *d,
  522. struct device_attribute *attr, const char *buf, size_t count)
  523. {
  524. unsigned long tmp;
  525. unsigned char val;
  526. unsigned long flags;
  527. sscanf(buf, "%lX", &tmp);
  528. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  529. val = (unsigned char)tmp;
  530. spin_lock_irqsave(&event_lock, flags);
  531. SET_PORT_BITS(TLCLK_REG0, 0x7f, val);
  532. spin_unlock_irqrestore(&event_lock, flags);
  533. return strnlen(buf, count);
  534. }
  535. static DEVICE_ATTR(hardware_switching, (S_IWUSR|S_IWGRP), NULL,
  536. store_hardware_switching);
  537. static ssize_t store_refalign (struct device *d,
  538. struct device_attribute *attr, const char *buf, size_t count)
  539. {
  540. unsigned long tmp;
  541. unsigned long flags;
  542. sscanf(buf, "%lX", &tmp);
  543. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  544. spin_lock_irqsave(&event_lock, flags);
  545. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
  546. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0x08);
  547. SET_PORT_BITS(TLCLK_REG0, 0xf7, 0);
  548. spin_unlock_irqrestore(&event_lock, flags);
  549. return strnlen(buf, count);
  550. }
  551. static DEVICE_ATTR(refalign, (S_IWUSR|S_IWGRP), NULL, store_refalign);
  552. static ssize_t store_mode_select (struct device *d,
  553. struct device_attribute *attr, const char *buf, size_t count)
  554. {
  555. unsigned long tmp;
  556. unsigned char val;
  557. unsigned long flags;
  558. sscanf(buf, "%lX", &tmp);
  559. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  560. val = (unsigned char)tmp;
  561. spin_lock_irqsave(&event_lock, flags);
  562. SET_PORT_BITS(TLCLK_REG0, 0xcf, val);
  563. spin_unlock_irqrestore(&event_lock, flags);
  564. return strnlen(buf, count);
  565. }
  566. static DEVICE_ATTR(mode_select, (S_IWUSR|S_IWGRP), NULL, store_mode_select);
  567. static ssize_t store_reset (struct device *d,
  568. struct device_attribute *attr, const char *buf, size_t count)
  569. {
  570. unsigned long tmp;
  571. unsigned char val;
  572. unsigned long flags;
  573. sscanf(buf, "%lX", &tmp);
  574. dev_dbg(d, "tmp = 0x%lX\n", tmp);
  575. val = (unsigned char)tmp;
  576. spin_lock_irqsave(&event_lock, flags);
  577. SET_PORT_BITS(TLCLK_REG4, 0xfd, val);
  578. spin_unlock_irqrestore(&event_lock, flags);
  579. return strnlen(buf, count);
  580. }
  581. static DEVICE_ATTR(reset, (S_IWUSR|S_IWGRP), NULL, store_reset);
  582. static struct attribute *tlclk_sysfs_entries[] = {
  583. &dev_attr_current_ref.attr,
  584. &dev_attr_telclock_version.attr,
  585. &dev_attr_alarms.attr,
  586. &dev_attr_received_ref_clk3a.attr,
  587. &dev_attr_received_ref_clk3b.attr,
  588. &dev_attr_enable_clk3a_output.attr,
  589. &dev_attr_enable_clk3b_output.attr,
  590. &dev_attr_enable_clkb1_output.attr,
  591. &dev_attr_enable_clka1_output.attr,
  592. &dev_attr_enable_clkb0_output.attr,
  593. &dev_attr_enable_clka0_output.attr,
  594. &dev_attr_select_amcb1_transmit_clock.attr,
  595. &dev_attr_select_amcb2_transmit_clock.attr,
  596. &dev_attr_select_redundant_clock.attr,
  597. &dev_attr_select_ref_frequency.attr,
  598. &dev_attr_filter_select.attr,
  599. &dev_attr_hardware_switching_mode.attr,
  600. &dev_attr_hardware_switching.attr,
  601. &dev_attr_refalign.attr,
  602. &dev_attr_mode_select.attr,
  603. &dev_attr_reset.attr,
  604. NULL
  605. };
  606. static struct attribute_group tlclk_attribute_group = {
  607. .name = NULL, /* put in device directory */
  608. .attrs = tlclk_sysfs_entries,
  609. };
  610. static struct platform_device *tlclk_device;
  611. static int __init tlclk_init(void)
  612. {
  613. int ret;
  614. ret = register_chrdev(tlclk_major, "telco_clock", &tlclk_fops);
  615. if (ret < 0) {
  616. printk(KERN_ERR "tlclk: can't get major %d.\n", tlclk_major);
  617. return ret;
  618. }
  619. tlclk_major = ret;
  620. alarm_events = kzalloc( sizeof(struct tlclk_alarms), GFP_KERNEL);
  621. if (!alarm_events)
  622. goto out1;
  623. /* Read telecom clock IRQ number (Set by BIOS) */
  624. if (!request_region(TLCLK_BASE, 8, "telco_clock")) {
  625. printk(KERN_ERR "tlclk: request_region 0x%X failed.\n",
  626. TLCLK_BASE);
  627. ret = -EBUSY;
  628. goto out2;
  629. }
  630. telclk_interrupt = (inb(TLCLK_REG7) & 0x0f);
  631. if (0x0F == telclk_interrupt ) { /* not MCPBL0010 ? */
  632. printk(KERN_ERR "telclk_interrup = 0x%x non-mcpbl0010 hw.\n",
  633. telclk_interrupt);
  634. ret = -ENXIO;
  635. goto out3;
  636. }
  637. init_timer(&switchover_timer);
  638. ret = misc_register(&tlclk_miscdev);
  639. if (ret < 0) {
  640. printk(KERN_ERR "tlclk: misc_register returns %d.\n", ret);
  641. goto out3;
  642. }
  643. tlclk_device = platform_device_register_simple("telco_clock",
  644. -1, NULL, 0);
  645. if (IS_ERR(tlclk_device)) {
  646. printk(KERN_ERR "tlclk: platform_device_register failed.\n");
  647. ret = PTR_ERR(tlclk_device);
  648. goto out4;
  649. }
  650. ret = sysfs_create_group(&tlclk_device->dev.kobj,
  651. &tlclk_attribute_group);
  652. if (ret) {
  653. printk(KERN_ERR "tlclk: failed to create sysfs device attributes.\n");
  654. goto out5;
  655. }
  656. return 0;
  657. out5:
  658. platform_device_unregister(tlclk_device);
  659. out4:
  660. misc_deregister(&tlclk_miscdev);
  661. out3:
  662. release_region(TLCLK_BASE, 8);
  663. out2:
  664. kfree(alarm_events);
  665. out1:
  666. unregister_chrdev(tlclk_major, "telco_clock");
  667. return ret;
  668. }
  669. static void __exit tlclk_cleanup(void)
  670. {
  671. sysfs_remove_group(&tlclk_device->dev.kobj, &tlclk_attribute_group);
  672. platform_device_unregister(tlclk_device);
  673. misc_deregister(&tlclk_miscdev);
  674. unregister_chrdev(tlclk_major, "telco_clock");
  675. release_region(TLCLK_BASE, 8);
  676. del_timer_sync(&switchover_timer);
  677. kfree(alarm_events);
  678. }
  679. static void switchover_timeout(unsigned long data)
  680. {
  681. unsigned long flags = *(unsigned long *) data;
  682. if ((flags & 1)) {
  683. if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
  684. alarm_events->switchover_primary++;
  685. } else {
  686. if ((inb(TLCLK_REG1) & 0x08) != (flags & 0x08))
  687. alarm_events->switchover_secondary++;
  688. }
  689. /* Alarm processing is done, wake up read task */
  690. del_timer(&switchover_timer);
  691. got_event = 1;
  692. wake_up(&wq);
  693. }
  694. static irqreturn_t tlclk_interrupt(int irq, void *dev_id)
  695. {
  696. unsigned long flags;
  697. spin_lock_irqsave(&event_lock, flags);
  698. /* Read and clear interrupt events */
  699. int_events = inb(TLCLK_REG6);
  700. /* Primary_Los changed from 0 to 1 ? */
  701. if (int_events & PRI_LOS_01_MASK) {
  702. if (inb(TLCLK_REG2) & SEC_LOST_MASK)
  703. alarm_events->lost_clocks++;
  704. else
  705. alarm_events->lost_primary_clock++;
  706. }
  707. /* Primary_Los changed from 1 to 0 ? */
  708. if (int_events & PRI_LOS_10_MASK) {
  709. alarm_events->primary_clock_back++;
  710. SET_PORT_BITS(TLCLK_REG1, 0xFE, 1);
  711. }
  712. /* Secondary_Los changed from 0 to 1 ? */
  713. if (int_events & SEC_LOS_01_MASK) {
  714. if (inb(TLCLK_REG2) & PRI_LOST_MASK)
  715. alarm_events->lost_clocks++;
  716. else
  717. alarm_events->lost_secondary_clock++;
  718. }
  719. /* Secondary_Los changed from 1 to 0 ? */
  720. if (int_events & SEC_LOS_10_MASK) {
  721. alarm_events->secondary_clock_back++;
  722. SET_PORT_BITS(TLCLK_REG1, 0xFE, 0);
  723. }
  724. if (int_events & HOLDOVER_10_MASK)
  725. alarm_events->pll_end_holdover++;
  726. if (int_events & UNLOCK_01_MASK)
  727. alarm_events->pll_lost_sync++;
  728. if (int_events & UNLOCK_10_MASK)
  729. alarm_events->pll_sync++;
  730. /* Holdover changed from 0 to 1 ? */
  731. if (int_events & HOLDOVER_01_MASK) {
  732. alarm_events->pll_holdover++;
  733. /* TIMEOUT in ~10ms */
  734. switchover_timer.expires = jiffies + msecs_to_jiffies(10);
  735. tlclk_timer_data = inb(TLCLK_REG1);
  736. switchover_timer.data = (unsigned long) &tlclk_timer_data;
  737. mod_timer(&switchover_timer, switchover_timer.expires);
  738. } else {
  739. got_event = 1;
  740. wake_up(&wq);
  741. }
  742. spin_unlock_irqrestore(&event_lock, flags);
  743. return IRQ_HANDLED;
  744. }
  745. module_init(tlclk_init);
  746. module_exit(tlclk_cleanup);