Loading...
1/*
2 * pti.c - PTI driver for cJTAG data extration
3 *
4 * Copyright (C) Intel 2010
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 *
17 * The PTI (Parallel Trace Interface) driver directs trace data routed from
18 * various parts in the system out through the Intel Penwell PTI port and
19 * out of the mobile device for analysis with a debugging tool
20 * (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7,
21 * compact JTAG, standard.
22 */
23
24#include <linux/init.h>
25#include <linux/sched.h>
26#include <linux/interrupt.h>
27#include <linux/console.h>
28#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/pci.h>
33#include <linux/mutex.h>
34#include <linux/miscdevice.h>
35#include <linux/pti.h>
36#include <linux/slab.h>
37#include <linux/uaccess.h>
38
39#define DRIVERNAME "pti"
40#define PCINAME "pciPTI"
41#define TTYNAME "ttyPTI"
42#define CHARNAME "pti"
43#define PTITTY_MINOR_START 0
44#define PTITTY_MINOR_NUM 2
45#define MAX_APP_IDS 16 /* 128 channel ids / u8 bit size */
46#define MAX_OS_IDS 16 /* 128 channel ids / u8 bit size */
47#define MAX_MODEM_IDS 16 /* 128 channel ids / u8 bit size */
48#define MODEM_BASE_ID 71 /* modem master ID address */
49#define CONTROL_ID 72 /* control master ID address */
50#define CONSOLE_ID 73 /* console master ID address */
51#define OS_BASE_ID 74 /* base OS master ID address */
52#define APP_BASE_ID 80 /* base App master ID address */
53#define CONTROL_FRAME_LEN 32 /* PTI control frame maximum size */
54#define USER_COPY_SIZE 8192 /* 8Kb buffer for user space copy */
55#define APERTURE_14 0x3800000 /* offset to first OS write addr */
56#define APERTURE_LEN 0x400000 /* address length */
57
58struct pti_tty {
59 struct pti_masterchannel *mc;
60};
61
62struct pti_dev {
63 struct tty_port port;
64 unsigned long pti_addr;
65 unsigned long aperture_base;
66 void __iomem *pti_ioaddr;
67 u8 ia_app[MAX_APP_IDS];
68 u8 ia_os[MAX_OS_IDS];
69 u8 ia_modem[MAX_MODEM_IDS];
70};
71
72/*
73 * This protects access to ia_app, ia_os, and ia_modem,
74 * which keeps track of channels allocated in
75 * an aperture write id.
76 */
77static DEFINE_MUTEX(alloclock);
78
79static struct pci_device_id pci_ids[] __devinitconst = {
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B)},
81 {0}
82};
83
84static struct tty_driver *pti_tty_driver;
85static struct pti_dev *drv_data;
86
87static unsigned int pti_console_channel;
88static unsigned int pti_control_channel;
89
90/**
91 * pti_write_to_aperture()- The private write function to PTI HW.
92 *
93 * @mc: The 'aperture'. It's part of a write address that holds
94 * a master and channel ID.
95 * @buf: Data being written to the HW that will ultimately be seen
96 * in a debugging tool (Fido, Lauterbach).
97 * @len: Size of buffer.
98 *
99 * Since each aperture is specified by a unique
100 * master/channel ID, no two processes will be writing
101 * to the same aperture at the same time so no lock is required. The
102 * PTI-Output agent will send these out in the order that they arrived, and
103 * thus, it will intermix these messages. The debug tool can then later
104 * regroup the appropriate message segments together reconstituting each
105 * message.
106 */
107static void pti_write_to_aperture(struct pti_masterchannel *mc,
108 u8 *buf,
109 int len)
110{
111 int dwordcnt;
112 int final;
113 int i;
114 u32 ptiword;
115 u32 __iomem *aperture;
116 u8 *p = buf;
117
118 /*
119 * calculate the aperture offset from the base using the master and
120 * channel id's.
121 */
122 aperture = drv_data->pti_ioaddr + (mc->master << 15)
123 + (mc->channel << 8);
124
125 dwordcnt = len >> 2;
126 final = len - (dwordcnt << 2); /* final = trailing bytes */
127 if (final == 0 && dwordcnt != 0) { /* always need a final dword */
128 final += 4;
129 dwordcnt--;
130 }
131
132 for (i = 0; i < dwordcnt; i++) {
133 ptiword = be32_to_cpu(*(u32 *)p);
134 p += 4;
135 iowrite32(ptiword, aperture);
136 }
137
138 aperture += PTI_LASTDWORD_DTS; /* adding DTS signals that is EOM */
139
140 ptiword = 0;
141 for (i = 0; i < final; i++)
142 ptiword |= *p++ << (24-(8*i));
143
144 iowrite32(ptiword, aperture);
145 return;
146}
147
148/**
149 * pti_control_frame_built_and_sent()- control frame build and send function.
150 *
151 * @mc: The master / channel structure on which the function
152 * built a control frame.
153 * @thread_name: The thread name associated with the master / channel or
154 * 'NULL' if using the 'current' global variable.
155 *
156 * To be able to post process the PTI contents on host side, a control frame
157 * is added before sending any PTI content. So the host side knows on
158 * each PTI frame the name of the thread using a dedicated master / channel.
159 * The thread name is retrieved from 'current' global variable if 'thread_name'
160 * is 'NULL', else it is retrieved from 'thread_name' parameter.
161 * This function builds this frame and sends it to a master ID CONTROL_ID.
162 * The overhead is only 32 bytes since the driver only writes to HW
163 * in 32 byte chunks.
164 */
165static void pti_control_frame_built_and_sent(struct pti_masterchannel *mc,
166 const char *thread_name)
167{
168 /*
169 * Since we access the comm member in current's task_struct, we only
170 * need to be as large as what 'comm' in that structure is.
171 */
172 char comm[TASK_COMM_LEN];
173 struct pti_masterchannel mccontrol = {.master = CONTROL_ID,
174 .channel = 0};
175 const char *thread_name_p;
176 const char *control_format = "%3d %3d %s";
177 u8 control_frame[CONTROL_FRAME_LEN];
178
179 if (!thread_name) {
180 if (!in_interrupt())
181 get_task_comm(comm, current);
182 else
183 strncpy(comm, "Interrupt", TASK_COMM_LEN);
184
185 /* Absolutely ensure our buffer is zero terminated. */
186 comm[TASK_COMM_LEN-1] = 0;
187 thread_name_p = comm;
188 } else {
189 thread_name_p = thread_name;
190 }
191
192 mccontrol.channel = pti_control_channel;
193 pti_control_channel = (pti_control_channel + 1) & 0x7f;
194
195 snprintf(control_frame, CONTROL_FRAME_LEN, control_format, mc->master,
196 mc->channel, thread_name_p);
197 pti_write_to_aperture(&mccontrol, control_frame, strlen(control_frame));
198}
199
200/**
201 * pti_write_full_frame_to_aperture()- high level function to
202 * write to PTI.
203 *
204 * @mc: The 'aperture'. It's part of a write address that holds
205 * a master and channel ID.
206 * @buf: Data being written to the HW that will ultimately be seen
207 * in a debugging tool (Fido, Lauterbach).
208 * @len: Size of buffer.
209 *
210 * All threads sending data (either console, user space application, ...)
211 * are calling the high level function to write to PTI meaning that it is
212 * possible to add a control frame before sending the content.
213 */
214static void pti_write_full_frame_to_aperture(struct pti_masterchannel *mc,
215 const unsigned char *buf,
216 int len)
217{
218 pti_control_frame_built_and_sent(mc, NULL);
219 pti_write_to_aperture(mc, (u8 *)buf, len);
220}
221
222/**
223 * get_id()- Allocate a master and channel ID.
224 *
225 * @id_array: an array of bits representing what channel
226 * id's are allocated for writing.
227 * @max_ids: The max amount of available write IDs to use.
228 * @base_id: The starting SW channel ID, based on the Intel
229 * PTI arch.
230 * @thread_name: The thread name associated with the master / channel or
231 * 'NULL' if using the 'current' global variable.
232 *
233 * Returns:
234 * pti_masterchannel struct with master, channel ID address
235 * 0 for error
236 *
237 * Each bit in the arrays ia_app and ia_os correspond to a master and
238 * channel id. The bit is one if the id is taken and 0 if free. For
239 * every master there are 128 channel id's.
240 */
241static struct pti_masterchannel *get_id(u8 *id_array,
242 int max_ids,
243 int base_id,
244 const char *thread_name)
245{
246 struct pti_masterchannel *mc;
247 int i, j, mask;
248
249 mc = kmalloc(sizeof(struct pti_masterchannel), GFP_KERNEL);
250 if (mc == NULL)
251 return NULL;
252
253 /* look for a byte with a free bit */
254 for (i = 0; i < max_ids; i++)
255 if (id_array[i] != 0xff)
256 break;
257 if (i == max_ids) {
258 kfree(mc);
259 return NULL;
260 }
261 /* find the bit in the 128 possible channel opportunities */
262 mask = 0x80;
263 for (j = 0; j < 8; j++) {
264 if ((id_array[i] & mask) == 0)
265 break;
266 mask >>= 1;
267 }
268
269 /* grab it */
270 id_array[i] |= mask;
271 mc->master = base_id;
272 mc->channel = ((i & 0xf)<<3) + j;
273 /* write new master Id / channel Id allocation to channel control */
274 pti_control_frame_built_and_sent(mc, thread_name);
275 return mc;
276}
277
278/*
279 * The following three functions:
280 * pti_request_mastercahannel(), mipi_release_masterchannel()
281 * and pti_writedata() are an API for other kernel drivers to
282 * access PTI.
283 */
284
285/**
286 * pti_request_masterchannel()- Kernel API function used to allocate
287 * a master, channel ID address
288 * to write to PTI HW.
289 *
290 * @type: 0- request Application master, channel aperture ID
291 * write address.
292 * 1- request OS master, channel aperture ID write
293 * address.
294 * 2- request Modem master, channel aperture ID
295 * write address.
296 * Other values, error.
297 * @thread_name: The thread name associated with the master / channel or
298 * 'NULL' if using the 'current' global variable.
299 *
300 * Returns:
301 * pti_masterchannel struct
302 * 0 for error
303 */
304struct pti_masterchannel *pti_request_masterchannel(u8 type,
305 const char *thread_name)
306{
307 struct pti_masterchannel *mc;
308
309 mutex_lock(&alloclock);
310
311 switch (type) {
312
313 case 0:
314 mc = get_id(drv_data->ia_app, MAX_APP_IDS,
315 APP_BASE_ID, thread_name);
316 break;
317
318 case 1:
319 mc = get_id(drv_data->ia_os, MAX_OS_IDS,
320 OS_BASE_ID, thread_name);
321 break;
322
323 case 2:
324 mc = get_id(drv_data->ia_modem, MAX_MODEM_IDS,
325 MODEM_BASE_ID, thread_name);
326 break;
327 default:
328 mc = NULL;
329 }
330
331 mutex_unlock(&alloclock);
332 return mc;
333}
334EXPORT_SYMBOL_GPL(pti_request_masterchannel);
335
336/**
337 * pti_release_masterchannel()- Kernel API function used to release
338 * a master, channel ID address
339 * used to write to PTI HW.
340 *
341 * @mc: master, channel apeture ID address to be released. This
342 * will de-allocate the structure via kfree().
343 */
344void pti_release_masterchannel(struct pti_masterchannel *mc)
345{
346 u8 master, channel, i;
347
348 mutex_lock(&alloclock);
349
350 if (mc) {
351 master = mc->master;
352 channel = mc->channel;
353
354 if (master == APP_BASE_ID) {
355 i = channel >> 3;
356 drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7));
357 } else if (master == OS_BASE_ID) {
358 i = channel >> 3;
359 drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7));
360 } else {
361 i = channel >> 3;
362 drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7));
363 }
364
365 kfree(mc);
366 }
367
368 mutex_unlock(&alloclock);
369}
370EXPORT_SYMBOL_GPL(pti_release_masterchannel);
371
372/**
373 * pti_writedata()- Kernel API function used to write trace
374 * debugging data to PTI HW.
375 *
376 * @mc: Master, channel aperture ID address to write to.
377 * Null value will return with no write occurring.
378 * @buf: Trace debuging data to write to the PTI HW.
379 * Null value will return with no write occurring.
380 * @count: Size of buf. Value of 0 or a negative number will
381 * return with no write occuring.
382 */
383void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count)
384{
385 /*
386 * since this function is exported, this is treated like an
387 * API function, thus, all parameters should
388 * be checked for validity.
389 */
390 if ((mc != NULL) && (buf != NULL) && (count > 0))
391 pti_write_to_aperture(mc, buf, count);
392 return;
393}
394EXPORT_SYMBOL_GPL(pti_writedata);
395
396/**
397 * pti_pci_remove()- Driver exit method to remove PTI from
398 * PCI bus.
399 * @pdev: variable containing pci info of PTI.
400 */
401static void __devexit pti_pci_remove(struct pci_dev *pdev)
402{
403 struct pti_dev *drv_data;
404
405 drv_data = pci_get_drvdata(pdev);
406 if (drv_data != NULL) {
407 pci_iounmap(pdev, drv_data->pti_ioaddr);
408 pci_set_drvdata(pdev, NULL);
409 kfree(drv_data);
410 pci_release_region(pdev, 1);
411 pci_disable_device(pdev);
412 }
413}
414
415/*
416 * for the tty_driver_*() basic function descriptions, see tty_driver.h.
417 * Specific header comments made for PTI-related specifics.
418 */
419
420/**
421 * pti_tty_driver_open()- Open an Application master, channel aperture
422 * ID to the PTI device via tty device.
423 *
424 * @tty: tty interface.
425 * @filp: filp interface pased to tty_port_open() call.
426 *
427 * Returns:
428 * int, 0 for success
429 * otherwise, fail value
430 *
431 * The main purpose of using the tty device interface is for
432 * each tty port to have a unique PTI write aperture. In an
433 * example use case, ttyPTI0 gets syslogd and an APP aperture
434 * ID and ttyPTI1 is where the n_tracesink ldisc hooks to route
435 * modem messages into PTI. Modem trace data does not have to
436 * go to ttyPTI1, but ttyPTI0 and ttyPTI1 do need to be distinct
437 * master IDs. These messages go through the PTI HW and out of
438 * the handheld platform and to the Fido/Lauterbach device.
439 */
440static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)
441{
442 /*
443 * we actually want to allocate a new channel per open, per
444 * system arch. HW gives more than plenty channels for a single
445 * system task to have its own channel to write trace data. This
446 * also removes a locking requirement for the actual write
447 * procedure.
448 */
449 return tty_port_open(&drv_data->port, tty, filp);
450}
451
452/**
453 * pti_tty_driver_close()- close tty device and release Application
454 * master, channel aperture ID to the PTI device via tty device.
455 *
456 * @tty: tty interface.
457 * @filp: filp interface pased to tty_port_close() call.
458 *
459 * The main purpose of using the tty device interface is to route
460 * syslog daemon messages to the PTI HW and out of the handheld platform
461 * and to the Fido/Lauterbach device.
462 */
463static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)
464{
465 tty_port_close(&drv_data->port, tty, filp);
466}
467
468/**
469 * pti_tty_install()- Used to set up specific master-channels
470 * to tty ports for organizational purposes when
471 * tracing viewed from debuging tools.
472 *
473 * @driver: tty driver information.
474 * @tty: tty struct containing pti information.
475 *
476 * Returns:
477 * 0 for success
478 * otherwise, error
479 */
480static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)
481{
482 int idx = tty->index;
483 struct pti_tty *pti_tty_data;
484 int ret = tty_init_termios(tty);
485
486 if (ret == 0) {
487 tty_driver_kref_get(driver);
488 tty->count++;
489 driver->ttys[idx] = tty;
490
491 pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);
492 if (pti_tty_data == NULL)
493 return -ENOMEM;
494
495 if (idx == PTITTY_MINOR_START)
496 pti_tty_data->mc = pti_request_masterchannel(0, NULL);
497 else
498 pti_tty_data->mc = pti_request_masterchannel(2, NULL);
499
500 if (pti_tty_data->mc == NULL) {
501 kfree(pti_tty_data);
502 return -ENXIO;
503 }
504 tty->driver_data = pti_tty_data;
505 }
506
507 return ret;
508}
509
510/**
511 * pti_tty_cleanup()- Used to de-allocate master-channel resources
512 * tied to tty's of this driver.
513 *
514 * @tty: tty struct containing pti information.
515 */
516static void pti_tty_cleanup(struct tty_struct *tty)
517{
518 struct pti_tty *pti_tty_data = tty->driver_data;
519 if (pti_tty_data == NULL)
520 return;
521 pti_release_masterchannel(pti_tty_data->mc);
522 kfree(pti_tty_data);
523 tty->driver_data = NULL;
524}
525
526/**
527 * pti_tty_driver_write()- Write trace debugging data through the char
528 * interface to the PTI HW. Part of the misc device implementation.
529 *
530 * @filp: Contains private data which is used to obtain
531 * master, channel write ID.
532 * @data: trace data to be written.
533 * @len: # of byte to write.
534 *
535 * Returns:
536 * int, # of bytes written
537 * otherwise, error
538 */
539static int pti_tty_driver_write(struct tty_struct *tty,
540 const unsigned char *buf, int len)
541{
542 struct pti_tty *pti_tty_data = tty->driver_data;
543 if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) {
544 pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len);
545 return len;
546 }
547 /*
548 * we can't write to the pti hardware if the private driver_data
549 * and the mc address is not there.
550 */
551 else
552 return -EFAULT;
553}
554
555/**
556 * pti_tty_write_room()- Always returns 2048.
557 *
558 * @tty: contains tty info of the pti driver.
559 */
560static int pti_tty_write_room(struct tty_struct *tty)
561{
562 return 2048;
563}
564
565/**
566 * pti_char_open()- Open an Application master, channel aperture
567 * ID to the PTI device. Part of the misc device implementation.
568 *
569 * @inode: not used.
570 * @filp: Output- will have a masterchannel struct set containing
571 * the allocated application PTI aperture write address.
572 *
573 * Returns:
574 * int, 0 for success
575 * otherwise, a fail value
576 */
577static int pti_char_open(struct inode *inode, struct file *filp)
578{
579 struct pti_masterchannel *mc;
580
581 /*
582 * We really do want to fail immediately if
583 * pti_request_masterchannel() fails,
584 * before assigning the value to filp->private_data.
585 * Slightly easier to debug if this driver needs debugging.
586 */
587 mc = pti_request_masterchannel(0, NULL);
588 if (mc == NULL)
589 return -ENOMEM;
590 filp->private_data = mc;
591 return 0;
592}
593
594/**
595 * pti_char_release()- Close a char channel to the PTI device. Part
596 * of the misc device implementation.
597 *
598 * @inode: Not used in this implementaiton.
599 * @filp: Contains private_data that contains the master, channel
600 * ID to be released by the PTI device.
601 *
602 * Returns:
603 * always 0
604 */
605static int pti_char_release(struct inode *inode, struct file *filp)
606{
607 pti_release_masterchannel(filp->private_data);
608 filp->private_data = NULL;
609 return 0;
610}
611
612/**
613 * pti_char_write()- Write trace debugging data through the char
614 * interface to the PTI HW. Part of the misc device implementation.
615 *
616 * @filp: Contains private data which is used to obtain
617 * master, channel write ID.
618 * @data: trace data to be written.
619 * @len: # of byte to write.
620 * @ppose: Not used in this function implementation.
621 *
622 * Returns:
623 * int, # of bytes written
624 * otherwise, error value
625 *
626 * Notes: From side discussions with Alan Cox and experimenting
627 * with PTI debug HW like Nokia's Fido box and Lauterbach
628 * devices, 8192 byte write buffer used by USER_COPY_SIZE was
629 * deemed an appropriate size for this type of usage with
630 * debugging HW.
631 */
632static ssize_t pti_char_write(struct file *filp, const char __user *data,
633 size_t len, loff_t *ppose)
634{
635 struct pti_masterchannel *mc;
636 void *kbuf;
637 const char __user *tmp;
638 size_t size = USER_COPY_SIZE;
639 size_t n = 0;
640
641 tmp = data;
642 mc = filp->private_data;
643
644 kbuf = kmalloc(size, GFP_KERNEL);
645 if (kbuf == NULL) {
646 pr_err("%s(%d): buf allocation failed\n",
647 __func__, __LINE__);
648 return -ENOMEM;
649 }
650
651 do {
652 if (len - n > USER_COPY_SIZE)
653 size = USER_COPY_SIZE;
654 else
655 size = len - n;
656
657 if (copy_from_user(kbuf, tmp, size)) {
658 kfree(kbuf);
659 return n ? n : -EFAULT;
660 }
661
662 pti_write_to_aperture(mc, kbuf, size);
663 n += size;
664 tmp += size;
665
666 } while (len > n);
667
668 kfree(kbuf);
669 return len;
670}
671
672static const struct tty_operations pti_tty_driver_ops = {
673 .open = pti_tty_driver_open,
674 .close = pti_tty_driver_close,
675 .write = pti_tty_driver_write,
676 .write_room = pti_tty_write_room,
677 .install = pti_tty_install,
678 .cleanup = pti_tty_cleanup
679};
680
681static const struct file_operations pti_char_driver_ops = {
682 .owner = THIS_MODULE,
683 .write = pti_char_write,
684 .open = pti_char_open,
685 .release = pti_char_release,
686};
687
688static struct miscdevice pti_char_driver = {
689 .minor = MISC_DYNAMIC_MINOR,
690 .name = CHARNAME,
691 .fops = &pti_char_driver_ops
692};
693
694/**
695 * pti_console_write()- Write to the console that has been acquired.
696 *
697 * @c: Not used in this implementaiton.
698 * @buf: Data to be written.
699 * @len: Length of buf.
700 */
701static void pti_console_write(struct console *c, const char *buf, unsigned len)
702{
703 static struct pti_masterchannel mc = {.master = CONSOLE_ID,
704 .channel = 0};
705
706 mc.channel = pti_console_channel;
707 pti_console_channel = (pti_console_channel + 1) & 0x7f;
708
709 pti_write_full_frame_to_aperture(&mc, buf, len);
710}
711
712/**
713 * pti_console_device()- Return the driver tty structure and set the
714 * associated index implementation.
715 *
716 * @c: Console device of the driver.
717 * @index: index associated with c.
718 *
719 * Returns:
720 * always value of pti_tty_driver structure when this function
721 * is called.
722 */
723static struct tty_driver *pti_console_device(struct console *c, int *index)
724{
725 *index = c->index;
726 return pti_tty_driver;
727}
728
729/**
730 * pti_console_setup()- Initialize console variables used by the driver.
731 *
732 * @c: Not used.
733 * @opts: Not used.
734 *
735 * Returns:
736 * always 0.
737 */
738static int pti_console_setup(struct console *c, char *opts)
739{
740 pti_console_channel = 0;
741 pti_control_channel = 0;
742 return 0;
743}
744
745/*
746 * pti_console struct, used to capture OS printk()'s and shift
747 * out to the PTI device for debugging. This cannot be
748 * enabled upon boot because of the possibility of eating
749 * any serial console printk's (race condition discovered).
750 * The console should be enabled upon when the tty port is
751 * used for the first time. Since the primary purpose for
752 * the tty port is to hook up syslog to it, the tty port
753 * will be open for a really long time.
754 */
755static struct console pti_console = {
756 .name = TTYNAME,
757 .write = pti_console_write,
758 .device = pti_console_device,
759 .setup = pti_console_setup,
760 .flags = CON_PRINTBUFFER,
761 .index = 0,
762};
763
764/**
765 * pti_port_activate()- Used to start/initialize any items upon
766 * first opening of tty_port().
767 *
768 * @port- The tty port number of the PTI device.
769 * @tty- The tty struct associated with this device.
770 *
771 * Returns:
772 * always returns 0
773 *
774 * Notes: The primary purpose of the PTI tty port 0 is to hook
775 * the syslog daemon to it; thus this port will be open for a
776 * very long time.
777 */
778static int pti_port_activate(struct tty_port *port, struct tty_struct *tty)
779{
780 if (port->tty->index == PTITTY_MINOR_START)
781 console_start(&pti_console);
782 return 0;
783}
784
785/**
786 * pti_port_shutdown()- Used to stop/shutdown any items upon the
787 * last tty port close.
788 *
789 * @port- The tty port number of the PTI device.
790 *
791 * Notes: The primary purpose of the PTI tty port 0 is to hook
792 * the syslog daemon to it; thus this port will be open for a
793 * very long time.
794 */
795static void pti_port_shutdown(struct tty_port *port)
796{
797 if (port->tty->index == PTITTY_MINOR_START)
798 console_stop(&pti_console);
799}
800
801static const struct tty_port_operations tty_port_ops = {
802 .activate = pti_port_activate,
803 .shutdown = pti_port_shutdown,
804};
805
806/*
807 * Note the _probe() call sets everything up and ties the char and tty
808 * to successfully detecting the PTI device on the pci bus.
809 */
810
811/**
812 * pti_pci_probe()- Used to detect pti on the pci bus and set
813 * things up in the driver.
814 *
815 * @pdev- pci_dev struct values for pti.
816 * @ent- pci_device_id struct for pti driver.
817 *
818 * Returns:
819 * 0 for success
820 * otherwise, error
821 */
822static int __devinit pti_pci_probe(struct pci_dev *pdev,
823 const struct pci_device_id *ent)
824{
825 int retval = -EINVAL;
826 int pci_bar = 1;
827
828 dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,
829 __func__, __LINE__, pdev->vendor, pdev->device);
830
831 retval = misc_register(&pti_char_driver);
832 if (retval) {
833 pr_err("%s(%d): CHAR registration failed of pti driver\n",
834 __func__, __LINE__);
835 pr_err("%s(%d): Error value returned: %d\n",
836 __func__, __LINE__, retval);
837 return retval;
838 }
839
840 retval = pci_enable_device(pdev);
841 if (retval != 0) {
842 dev_err(&pdev->dev,
843 "%s: pci_enable_device() returned error %d\n",
844 __func__, retval);
845 return retval;
846 }
847
848 drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
849
850 if (drv_data == NULL) {
851 retval = -ENOMEM;
852 dev_err(&pdev->dev,
853 "%s(%d): kmalloc() returned NULL memory.\n",
854 __func__, __LINE__);
855 return retval;
856 }
857 drv_data->pti_addr = pci_resource_start(pdev, pci_bar);
858
859 retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
860 if (retval != 0) {
861 dev_err(&pdev->dev,
862 "%s(%d): pci_request_region() returned error %d\n",
863 __func__, __LINE__, retval);
864 kfree(drv_data);
865 return retval;
866 }
867 drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;
868 drv_data->pti_ioaddr =
869 ioremap_nocache((u32)drv_data->aperture_base,
870 APERTURE_LEN);
871 if (!drv_data->pti_ioaddr) {
872 pci_release_region(pdev, pci_bar);
873 retval = -ENOMEM;
874 kfree(drv_data);
875 return retval;
876 }
877
878 pci_set_drvdata(pdev, drv_data);
879
880 tty_port_init(&drv_data->port);
881 drv_data->port.ops = &tty_port_ops;
882
883 tty_register_device(pti_tty_driver, 0, &pdev->dev);
884 tty_register_device(pti_tty_driver, 1, &pdev->dev);
885
886 register_console(&pti_console);
887
888 return retval;
889}
890
891static struct pci_driver pti_pci_driver = {
892 .name = PCINAME,
893 .id_table = pci_ids,
894 .probe = pti_pci_probe,
895 .remove = pti_pci_remove,
896};
897
898/**
899 *
900 * pti_init()- Overall entry/init call to the pti driver.
901 * It starts the registration process with the kernel.
902 *
903 * Returns:
904 * int __init, 0 for success
905 * otherwise value is an error
906 *
907 */
908static int __init pti_init(void)
909{
910 int retval = -EINVAL;
911
912 /* First register module as tty device */
913
914 pti_tty_driver = alloc_tty_driver(1);
915 if (pti_tty_driver == NULL) {
916 pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",
917 __func__, __LINE__);
918 return -ENOMEM;
919 }
920
921 pti_tty_driver->owner = THIS_MODULE;
922 pti_tty_driver->magic = TTY_DRIVER_MAGIC;
923 pti_tty_driver->driver_name = DRIVERNAME;
924 pti_tty_driver->name = TTYNAME;
925 pti_tty_driver->major = 0;
926 pti_tty_driver->minor_start = PTITTY_MINOR_START;
927 pti_tty_driver->minor_num = PTITTY_MINOR_NUM;
928 pti_tty_driver->num = PTITTY_MINOR_NUM;
929 pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
930 pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS;
931 pti_tty_driver->flags = TTY_DRIVER_REAL_RAW |
932 TTY_DRIVER_DYNAMIC_DEV;
933 pti_tty_driver->init_termios = tty_std_termios;
934
935 tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);
936
937 retval = tty_register_driver(pti_tty_driver);
938 if (retval) {
939 pr_err("%s(%d): TTY registration failed of pti driver\n",
940 __func__, __LINE__);
941 pr_err("%s(%d): Error value returned: %d\n",
942 __func__, __LINE__, retval);
943
944 pti_tty_driver = NULL;
945 return retval;
946 }
947
948 retval = pci_register_driver(&pti_pci_driver);
949
950 if (retval) {
951 pr_err("%s(%d): PCI registration failed of pti driver\n",
952 __func__, __LINE__);
953 pr_err("%s(%d): Error value returned: %d\n",
954 __func__, __LINE__, retval);
955
956 tty_unregister_driver(pti_tty_driver);
957 pr_err("%s(%d): Unregistering TTY part of pti driver\n",
958 __func__, __LINE__);
959 pti_tty_driver = NULL;
960 return retval;
961 }
962
963 return retval;
964}
965
966/**
967 * pti_exit()- Unregisters this module as a tty and pci driver.
968 */
969static void __exit pti_exit(void)
970{
971 int retval;
972
973 tty_unregister_device(pti_tty_driver, 0);
974 tty_unregister_device(pti_tty_driver, 1);
975
976 retval = tty_unregister_driver(pti_tty_driver);
977 if (retval) {
978 pr_err("%s(%d): TTY unregistration failed of pti driver\n",
979 __func__, __LINE__);
980 pr_err("%s(%d): Error value returned: %d\n",
981 __func__, __LINE__, retval);
982 }
983
984 pci_unregister_driver(&pti_pci_driver);
985
986 retval = misc_deregister(&pti_char_driver);
987 if (retval) {
988 pr_err("%s(%d): CHAR unregistration failed of pti driver\n",
989 __func__, __LINE__);
990 pr_err("%s(%d): Error value returned: %d\n",
991 __func__, __LINE__, retval);
992 }
993
994 unregister_console(&pti_console);
995 return;
996}
997
998module_init(pti_init);
999module_exit(pti_exit);
1000
1001MODULE_LICENSE("GPL");
1002MODULE_AUTHOR("Ken Mills, Jay Freyensee");
1003MODULE_DESCRIPTION("PTI Driver");
1004
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * pti.c - PTI driver for cJTAG data extration
4 *
5 * Copyright (C) Intel 2010
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * The PTI (Parallel Trace Interface) driver directs trace data routed from
10 * various parts in the system out through the Intel Penwell PTI port and
11 * out of the mobile device for analysis with a debugging tool
12 * (Lauterbach, Fido). This is part of a solution for the MIPI P1149.7,
13 * compact JTAG, standard.
14 */
15
16#include <linux/init.h>
17#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/console.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/tty.h>
23#include <linux/tty_driver.h>
24#include <linux/pci.h>
25#include <linux/mutex.h>
26#include <linux/miscdevice.h>
27#include <linux/intel-pti.h>
28#include <linux/slab.h>
29#include <linux/uaccess.h>
30
31#define DRIVERNAME "pti"
32#define PCINAME "pciPTI"
33#define TTYNAME "ttyPTI"
34#define CHARNAME "pti"
35#define PTITTY_MINOR_START 0
36#define PTITTY_MINOR_NUM 2
37#define MAX_APP_IDS 16 /* 128 channel ids / u8 bit size */
38#define MAX_OS_IDS 16 /* 128 channel ids / u8 bit size */
39#define MAX_MODEM_IDS 16 /* 128 channel ids / u8 bit size */
40#define MODEM_BASE_ID 71 /* modem master ID address */
41#define CONTROL_ID 72 /* control master ID address */
42#define CONSOLE_ID 73 /* console master ID address */
43#define OS_BASE_ID 74 /* base OS master ID address */
44#define APP_BASE_ID 80 /* base App master ID address */
45#define CONTROL_FRAME_LEN 32 /* PTI control frame maximum size */
46#define USER_COPY_SIZE 8192 /* 8Kb buffer for user space copy */
47#define APERTURE_14 0x3800000 /* offset to first OS write addr */
48#define APERTURE_LEN 0x400000 /* address length */
49
50struct pti_tty {
51 struct pti_masterchannel *mc;
52};
53
54struct pti_dev {
55 struct tty_port port[PTITTY_MINOR_NUM];
56 unsigned long pti_addr;
57 unsigned long aperture_base;
58 void __iomem *pti_ioaddr;
59 u8 ia_app[MAX_APP_IDS];
60 u8 ia_os[MAX_OS_IDS];
61 u8 ia_modem[MAX_MODEM_IDS];
62};
63
64/*
65 * This protects access to ia_app, ia_os, and ia_modem,
66 * which keeps track of channels allocated in
67 * an aperture write id.
68 */
69static DEFINE_MUTEX(alloclock);
70
71static const struct pci_device_id pci_ids[] = {
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x82B)},
73 {0}
74};
75
76static struct tty_driver *pti_tty_driver;
77static struct pti_dev *drv_data;
78
79static unsigned int pti_console_channel;
80static unsigned int pti_control_channel;
81
82/**
83 * pti_write_to_aperture()- The private write function to PTI HW.
84 *
85 * @mc: The 'aperture'. It's part of a write address that holds
86 * a master and channel ID.
87 * @buf: Data being written to the HW that will ultimately be seen
88 * in a debugging tool (Fido, Lauterbach).
89 * @len: Size of buffer.
90 *
91 * Since each aperture is specified by a unique
92 * master/channel ID, no two processes will be writing
93 * to the same aperture at the same time so no lock is required. The
94 * PTI-Output agent will send these out in the order that they arrived, and
95 * thus, it will intermix these messages. The debug tool can then later
96 * regroup the appropriate message segments together reconstituting each
97 * message.
98 */
99static void pti_write_to_aperture(struct pti_masterchannel *mc,
100 u8 *buf,
101 int len)
102{
103 int dwordcnt;
104 int final;
105 int i;
106 u32 ptiword;
107 u32 __iomem *aperture;
108 u8 *p = buf;
109
110 /*
111 * calculate the aperture offset from the base using the master and
112 * channel id's.
113 */
114 aperture = drv_data->pti_ioaddr + (mc->master << 15)
115 + (mc->channel << 8);
116
117 dwordcnt = len >> 2;
118 final = len - (dwordcnt << 2); /* final = trailing bytes */
119 if (final == 0 && dwordcnt != 0) { /* always need a final dword */
120 final += 4;
121 dwordcnt--;
122 }
123
124 for (i = 0; i < dwordcnt; i++) {
125 ptiword = be32_to_cpu(*(u32 *)p);
126 p += 4;
127 iowrite32(ptiword, aperture);
128 }
129
130 aperture += PTI_LASTDWORD_DTS; /* adding DTS signals that is EOM */
131
132 ptiword = 0;
133 for (i = 0; i < final; i++)
134 ptiword |= *p++ << (24-(8*i));
135
136 iowrite32(ptiword, aperture);
137 return;
138}
139
140/**
141 * pti_control_frame_built_and_sent()- control frame build and send function.
142 *
143 * @mc: The master / channel structure on which the function
144 * built a control frame.
145 * @thread_name: The thread name associated with the master / channel or
146 * 'NULL' if using the 'current' global variable.
147 *
148 * To be able to post process the PTI contents on host side, a control frame
149 * is added before sending any PTI content. So the host side knows on
150 * each PTI frame the name of the thread using a dedicated master / channel.
151 * The thread name is retrieved from 'current' global variable if 'thread_name'
152 * is 'NULL', else it is retrieved from 'thread_name' parameter.
153 * This function builds this frame and sends it to a master ID CONTROL_ID.
154 * The overhead is only 32 bytes since the driver only writes to HW
155 * in 32 byte chunks.
156 */
157static void pti_control_frame_built_and_sent(struct pti_masterchannel *mc,
158 const char *thread_name)
159{
160 /*
161 * Since we access the comm member in current's task_struct, we only
162 * need to be as large as what 'comm' in that structure is.
163 */
164 char comm[TASK_COMM_LEN];
165 struct pti_masterchannel mccontrol = {.master = CONTROL_ID,
166 .channel = 0};
167 const char *thread_name_p;
168 const char *control_format = "%3d %3d %s";
169 u8 control_frame[CONTROL_FRAME_LEN];
170
171 if (!thread_name) {
172 if (!in_interrupt())
173 get_task_comm(comm, current);
174 else
175 strncpy(comm, "Interrupt", TASK_COMM_LEN);
176
177 /* Absolutely ensure our buffer is zero terminated. */
178 comm[TASK_COMM_LEN-1] = 0;
179 thread_name_p = comm;
180 } else {
181 thread_name_p = thread_name;
182 }
183
184 mccontrol.channel = pti_control_channel;
185 pti_control_channel = (pti_control_channel + 1) & 0x7f;
186
187 snprintf(control_frame, CONTROL_FRAME_LEN, control_format, mc->master,
188 mc->channel, thread_name_p);
189 pti_write_to_aperture(&mccontrol, control_frame, strlen(control_frame));
190}
191
192/**
193 * pti_write_full_frame_to_aperture()- high level function to
194 * write to PTI.
195 *
196 * @mc: The 'aperture'. It's part of a write address that holds
197 * a master and channel ID.
198 * @buf: Data being written to the HW that will ultimately be seen
199 * in a debugging tool (Fido, Lauterbach).
200 * @len: Size of buffer.
201 *
202 * All threads sending data (either console, user space application, ...)
203 * are calling the high level function to write to PTI meaning that it is
204 * possible to add a control frame before sending the content.
205 */
206static void pti_write_full_frame_to_aperture(struct pti_masterchannel *mc,
207 const unsigned char *buf,
208 int len)
209{
210 pti_control_frame_built_and_sent(mc, NULL);
211 pti_write_to_aperture(mc, (u8 *)buf, len);
212}
213
214/**
215 * get_id()- Allocate a master and channel ID.
216 *
217 * @id_array: an array of bits representing what channel
218 * id's are allocated for writing.
219 * @max_ids: The max amount of available write IDs to use.
220 * @base_id: The starting SW channel ID, based on the Intel
221 * PTI arch.
222 * @thread_name: The thread name associated with the master / channel or
223 * 'NULL' if using the 'current' global variable.
224 *
225 * Returns:
226 * pti_masterchannel struct with master, channel ID address
227 * 0 for error
228 *
229 * Each bit in the arrays ia_app and ia_os correspond to a master and
230 * channel id. The bit is one if the id is taken and 0 if free. For
231 * every master there are 128 channel id's.
232 */
233static struct pti_masterchannel *get_id(u8 *id_array,
234 int max_ids,
235 int base_id,
236 const char *thread_name)
237{
238 struct pti_masterchannel *mc;
239 int i, j, mask;
240
241 mc = kmalloc(sizeof(struct pti_masterchannel), GFP_KERNEL);
242 if (mc == NULL)
243 return NULL;
244
245 /* look for a byte with a free bit */
246 for (i = 0; i < max_ids; i++)
247 if (id_array[i] != 0xff)
248 break;
249 if (i == max_ids) {
250 kfree(mc);
251 return NULL;
252 }
253 /* find the bit in the 128 possible channel opportunities */
254 mask = 0x80;
255 for (j = 0; j < 8; j++) {
256 if ((id_array[i] & mask) == 0)
257 break;
258 mask >>= 1;
259 }
260
261 /* grab it */
262 id_array[i] |= mask;
263 mc->master = base_id;
264 mc->channel = ((i & 0xf)<<3) + j;
265 /* write new master Id / channel Id allocation to channel control */
266 pti_control_frame_built_and_sent(mc, thread_name);
267 return mc;
268}
269
270/*
271 * The following three functions:
272 * pti_request_mastercahannel(), mipi_release_masterchannel()
273 * and pti_writedata() are an API for other kernel drivers to
274 * access PTI.
275 */
276
277/**
278 * pti_request_masterchannel()- Kernel API function used to allocate
279 * a master, channel ID address
280 * to write to PTI HW.
281 *
282 * @type: 0- request Application master, channel aperture ID
283 * write address.
284 * 1- request OS master, channel aperture ID write
285 * address.
286 * 2- request Modem master, channel aperture ID
287 * write address.
288 * Other values, error.
289 * @thread_name: The thread name associated with the master / channel or
290 * 'NULL' if using the 'current' global variable.
291 *
292 * Returns:
293 * pti_masterchannel struct
294 * 0 for error
295 */
296struct pti_masterchannel *pti_request_masterchannel(u8 type,
297 const char *thread_name)
298{
299 struct pti_masterchannel *mc;
300
301 mutex_lock(&alloclock);
302
303 switch (type) {
304
305 case 0:
306 mc = get_id(drv_data->ia_app, MAX_APP_IDS,
307 APP_BASE_ID, thread_name);
308 break;
309
310 case 1:
311 mc = get_id(drv_data->ia_os, MAX_OS_IDS,
312 OS_BASE_ID, thread_name);
313 break;
314
315 case 2:
316 mc = get_id(drv_data->ia_modem, MAX_MODEM_IDS,
317 MODEM_BASE_ID, thread_name);
318 break;
319 default:
320 mc = NULL;
321 }
322
323 mutex_unlock(&alloclock);
324 return mc;
325}
326EXPORT_SYMBOL_GPL(pti_request_masterchannel);
327
328/**
329 * pti_release_masterchannel()- Kernel API function used to release
330 * a master, channel ID address
331 * used to write to PTI HW.
332 *
333 * @mc: master, channel apeture ID address to be released. This
334 * will de-allocate the structure via kfree().
335 */
336void pti_release_masterchannel(struct pti_masterchannel *mc)
337{
338 u8 master, channel, i;
339
340 mutex_lock(&alloclock);
341
342 if (mc) {
343 master = mc->master;
344 channel = mc->channel;
345
346 if (master == APP_BASE_ID) {
347 i = channel >> 3;
348 drv_data->ia_app[i] &= ~(0x80>>(channel & 0x7));
349 } else if (master == OS_BASE_ID) {
350 i = channel >> 3;
351 drv_data->ia_os[i] &= ~(0x80>>(channel & 0x7));
352 } else {
353 i = channel >> 3;
354 drv_data->ia_modem[i] &= ~(0x80>>(channel & 0x7));
355 }
356
357 kfree(mc);
358 }
359
360 mutex_unlock(&alloclock);
361}
362EXPORT_SYMBOL_GPL(pti_release_masterchannel);
363
364/**
365 * pti_writedata()- Kernel API function used to write trace
366 * debugging data to PTI HW.
367 *
368 * @mc: Master, channel aperture ID address to write to.
369 * Null value will return with no write occurring.
370 * @buf: Trace debuging data to write to the PTI HW.
371 * Null value will return with no write occurring.
372 * @count: Size of buf. Value of 0 or a negative number will
373 * return with no write occuring.
374 */
375void pti_writedata(struct pti_masterchannel *mc, u8 *buf, int count)
376{
377 /*
378 * since this function is exported, this is treated like an
379 * API function, thus, all parameters should
380 * be checked for validity.
381 */
382 if ((mc != NULL) && (buf != NULL) && (count > 0))
383 pti_write_to_aperture(mc, buf, count);
384 return;
385}
386EXPORT_SYMBOL_GPL(pti_writedata);
387
388/*
389 * for the tty_driver_*() basic function descriptions, see tty_driver.h.
390 * Specific header comments made for PTI-related specifics.
391 */
392
393/**
394 * pti_tty_driver_open()- Open an Application master, channel aperture
395 * ID to the PTI device via tty device.
396 *
397 * @tty: tty interface.
398 * @filp: filp interface pased to tty_port_open() call.
399 *
400 * Returns:
401 * int, 0 for success
402 * otherwise, fail value
403 *
404 * The main purpose of using the tty device interface is for
405 * each tty port to have a unique PTI write aperture. In an
406 * example use case, ttyPTI0 gets syslogd and an APP aperture
407 * ID and ttyPTI1 is where the n_tracesink ldisc hooks to route
408 * modem messages into PTI. Modem trace data does not have to
409 * go to ttyPTI1, but ttyPTI0 and ttyPTI1 do need to be distinct
410 * master IDs. These messages go through the PTI HW and out of
411 * the handheld platform and to the Fido/Lauterbach device.
412 */
413static int pti_tty_driver_open(struct tty_struct *tty, struct file *filp)
414{
415 /*
416 * we actually want to allocate a new channel per open, per
417 * system arch. HW gives more than plenty channels for a single
418 * system task to have its own channel to write trace data. This
419 * also removes a locking requirement for the actual write
420 * procedure.
421 */
422 return tty_port_open(tty->port, tty, filp);
423}
424
425/**
426 * pti_tty_driver_close()- close tty device and release Application
427 * master, channel aperture ID to the PTI device via tty device.
428 *
429 * @tty: tty interface.
430 * @filp: filp interface pased to tty_port_close() call.
431 *
432 * The main purpose of using the tty device interface is to route
433 * syslog daemon messages to the PTI HW and out of the handheld platform
434 * and to the Fido/Lauterbach device.
435 */
436static void pti_tty_driver_close(struct tty_struct *tty, struct file *filp)
437{
438 tty_port_close(tty->port, tty, filp);
439}
440
441/**
442 * pti_tty_install()- Used to set up specific master-channels
443 * to tty ports for organizational purposes when
444 * tracing viewed from debuging tools.
445 *
446 * @driver: tty driver information.
447 * @tty: tty struct containing pti information.
448 *
449 * Returns:
450 * 0 for success
451 * otherwise, error
452 */
453static int pti_tty_install(struct tty_driver *driver, struct tty_struct *tty)
454{
455 int idx = tty->index;
456 struct pti_tty *pti_tty_data;
457 int ret = tty_standard_install(driver, tty);
458
459 if (ret == 0) {
460 pti_tty_data = kmalloc(sizeof(struct pti_tty), GFP_KERNEL);
461 if (pti_tty_data == NULL)
462 return -ENOMEM;
463
464 if (idx == PTITTY_MINOR_START)
465 pti_tty_data->mc = pti_request_masterchannel(0, NULL);
466 else
467 pti_tty_data->mc = pti_request_masterchannel(2, NULL);
468
469 if (pti_tty_data->mc == NULL) {
470 kfree(pti_tty_data);
471 return -ENXIO;
472 }
473 tty->driver_data = pti_tty_data;
474 }
475
476 return ret;
477}
478
479/**
480 * pti_tty_cleanup()- Used to de-allocate master-channel resources
481 * tied to tty's of this driver.
482 *
483 * @tty: tty struct containing pti information.
484 */
485static void pti_tty_cleanup(struct tty_struct *tty)
486{
487 struct pti_tty *pti_tty_data = tty->driver_data;
488 if (pti_tty_data == NULL)
489 return;
490 pti_release_masterchannel(pti_tty_data->mc);
491 kfree(pti_tty_data);
492 tty->driver_data = NULL;
493}
494
495/**
496 * pti_tty_driver_write()- Write trace debugging data through the char
497 * interface to the PTI HW. Part of the misc device implementation.
498 *
499 * @filp: Contains private data which is used to obtain
500 * master, channel write ID.
501 * @data: trace data to be written.
502 * @len: # of byte to write.
503 *
504 * Returns:
505 * int, # of bytes written
506 * otherwise, error
507 */
508static int pti_tty_driver_write(struct tty_struct *tty,
509 const unsigned char *buf, int len)
510{
511 struct pti_tty *pti_tty_data = tty->driver_data;
512 if ((pti_tty_data != NULL) && (pti_tty_data->mc != NULL)) {
513 pti_write_to_aperture(pti_tty_data->mc, (u8 *)buf, len);
514 return len;
515 }
516 /*
517 * we can't write to the pti hardware if the private driver_data
518 * and the mc address is not there.
519 */
520 else
521 return -EFAULT;
522}
523
524/**
525 * pti_tty_write_room()- Always returns 2048.
526 *
527 * @tty: contains tty info of the pti driver.
528 */
529static int pti_tty_write_room(struct tty_struct *tty)
530{
531 return 2048;
532}
533
534/**
535 * pti_char_open()- Open an Application master, channel aperture
536 * ID to the PTI device. Part of the misc device implementation.
537 *
538 * @inode: not used.
539 * @filp: Output- will have a masterchannel struct set containing
540 * the allocated application PTI aperture write address.
541 *
542 * Returns:
543 * int, 0 for success
544 * otherwise, a fail value
545 */
546static int pti_char_open(struct inode *inode, struct file *filp)
547{
548 struct pti_masterchannel *mc;
549
550 /*
551 * We really do want to fail immediately if
552 * pti_request_masterchannel() fails,
553 * before assigning the value to filp->private_data.
554 * Slightly easier to debug if this driver needs debugging.
555 */
556 mc = pti_request_masterchannel(0, NULL);
557 if (mc == NULL)
558 return -ENOMEM;
559 filp->private_data = mc;
560 return 0;
561}
562
563/**
564 * pti_char_release()- Close a char channel to the PTI device. Part
565 * of the misc device implementation.
566 *
567 * @inode: Not used in this implementaiton.
568 * @filp: Contains private_data that contains the master, channel
569 * ID to be released by the PTI device.
570 *
571 * Returns:
572 * always 0
573 */
574static int pti_char_release(struct inode *inode, struct file *filp)
575{
576 pti_release_masterchannel(filp->private_data);
577 filp->private_data = NULL;
578 return 0;
579}
580
581/**
582 * pti_char_write()- Write trace debugging data through the char
583 * interface to the PTI HW. Part of the misc device implementation.
584 *
585 * @filp: Contains private data which is used to obtain
586 * master, channel write ID.
587 * @data: trace data to be written.
588 * @len: # of byte to write.
589 * @ppose: Not used in this function implementation.
590 *
591 * Returns:
592 * int, # of bytes written
593 * otherwise, error value
594 *
595 * Notes: From side discussions with Alan Cox and experimenting
596 * with PTI debug HW like Nokia's Fido box and Lauterbach
597 * devices, 8192 byte write buffer used by USER_COPY_SIZE was
598 * deemed an appropriate size for this type of usage with
599 * debugging HW.
600 */
601static ssize_t pti_char_write(struct file *filp, const char __user *data,
602 size_t len, loff_t *ppose)
603{
604 struct pti_masterchannel *mc;
605 void *kbuf;
606 const char __user *tmp;
607 size_t size = USER_COPY_SIZE;
608 size_t n = 0;
609
610 tmp = data;
611 mc = filp->private_data;
612
613 kbuf = kmalloc(size, GFP_KERNEL);
614 if (kbuf == NULL) {
615 pr_err("%s(%d): buf allocation failed\n",
616 __func__, __LINE__);
617 return -ENOMEM;
618 }
619
620 do {
621 if (len - n > USER_COPY_SIZE)
622 size = USER_COPY_SIZE;
623 else
624 size = len - n;
625
626 if (copy_from_user(kbuf, tmp, size)) {
627 kfree(kbuf);
628 return n ? n : -EFAULT;
629 }
630
631 pti_write_to_aperture(mc, kbuf, size);
632 n += size;
633 tmp += size;
634
635 } while (len > n);
636
637 kfree(kbuf);
638 return len;
639}
640
641static const struct tty_operations pti_tty_driver_ops = {
642 .open = pti_tty_driver_open,
643 .close = pti_tty_driver_close,
644 .write = pti_tty_driver_write,
645 .write_room = pti_tty_write_room,
646 .install = pti_tty_install,
647 .cleanup = pti_tty_cleanup
648};
649
650static const struct file_operations pti_char_driver_ops = {
651 .owner = THIS_MODULE,
652 .write = pti_char_write,
653 .open = pti_char_open,
654 .release = pti_char_release,
655};
656
657static struct miscdevice pti_char_driver = {
658 .minor = MISC_DYNAMIC_MINOR,
659 .name = CHARNAME,
660 .fops = &pti_char_driver_ops
661};
662
663/**
664 * pti_console_write()- Write to the console that has been acquired.
665 *
666 * @c: Not used in this implementaiton.
667 * @buf: Data to be written.
668 * @len: Length of buf.
669 */
670static void pti_console_write(struct console *c, const char *buf, unsigned len)
671{
672 static struct pti_masterchannel mc = {.master = CONSOLE_ID,
673 .channel = 0};
674
675 mc.channel = pti_console_channel;
676 pti_console_channel = (pti_console_channel + 1) & 0x7f;
677
678 pti_write_full_frame_to_aperture(&mc, buf, len);
679}
680
681/**
682 * pti_console_device()- Return the driver tty structure and set the
683 * associated index implementation.
684 *
685 * @c: Console device of the driver.
686 * @index: index associated with c.
687 *
688 * Returns:
689 * always value of pti_tty_driver structure when this function
690 * is called.
691 */
692static struct tty_driver *pti_console_device(struct console *c, int *index)
693{
694 *index = c->index;
695 return pti_tty_driver;
696}
697
698/**
699 * pti_console_setup()- Initialize console variables used by the driver.
700 *
701 * @c: Not used.
702 * @opts: Not used.
703 *
704 * Returns:
705 * always 0.
706 */
707static int pti_console_setup(struct console *c, char *opts)
708{
709 pti_console_channel = 0;
710 pti_control_channel = 0;
711 return 0;
712}
713
714/*
715 * pti_console struct, used to capture OS printk()'s and shift
716 * out to the PTI device for debugging. This cannot be
717 * enabled upon boot because of the possibility of eating
718 * any serial console printk's (race condition discovered).
719 * The console should be enabled upon when the tty port is
720 * used for the first time. Since the primary purpose for
721 * the tty port is to hook up syslog to it, the tty port
722 * will be open for a really long time.
723 */
724static struct console pti_console = {
725 .name = TTYNAME,
726 .write = pti_console_write,
727 .device = pti_console_device,
728 .setup = pti_console_setup,
729 .flags = CON_PRINTBUFFER,
730 .index = 0,
731};
732
733/**
734 * pti_port_activate()- Used to start/initialize any items upon
735 * first opening of tty_port().
736 *
737 * @port- The tty port number of the PTI device.
738 * @tty- The tty struct associated with this device.
739 *
740 * Returns:
741 * always returns 0
742 *
743 * Notes: The primary purpose of the PTI tty port 0 is to hook
744 * the syslog daemon to it; thus this port will be open for a
745 * very long time.
746 */
747static int pti_port_activate(struct tty_port *port, struct tty_struct *tty)
748{
749 if (port->tty->index == PTITTY_MINOR_START)
750 console_start(&pti_console);
751 return 0;
752}
753
754/**
755 * pti_port_shutdown()- Used to stop/shutdown any items upon the
756 * last tty port close.
757 *
758 * @port- The tty port number of the PTI device.
759 *
760 * Notes: The primary purpose of the PTI tty port 0 is to hook
761 * the syslog daemon to it; thus this port will be open for a
762 * very long time.
763 */
764static void pti_port_shutdown(struct tty_port *port)
765{
766 if (port->tty->index == PTITTY_MINOR_START)
767 console_stop(&pti_console);
768}
769
770static const struct tty_port_operations tty_port_ops = {
771 .activate = pti_port_activate,
772 .shutdown = pti_port_shutdown,
773};
774
775/*
776 * Note the _probe() call sets everything up and ties the char and tty
777 * to successfully detecting the PTI device on the pci bus.
778 */
779
780/**
781 * pti_pci_probe()- Used to detect pti on the pci bus and set
782 * things up in the driver.
783 *
784 * @pdev- pci_dev struct values for pti.
785 * @ent- pci_device_id struct for pti driver.
786 *
787 * Returns:
788 * 0 for success
789 * otherwise, error
790 */
791static int pti_pci_probe(struct pci_dev *pdev,
792 const struct pci_device_id *ent)
793{
794 unsigned int a;
795 int retval = -EINVAL;
796 int pci_bar = 1;
797
798 dev_dbg(&pdev->dev, "%s %s(%d): PTI PCI ID %04x:%04x\n", __FILE__,
799 __func__, __LINE__, pdev->vendor, pdev->device);
800
801 retval = misc_register(&pti_char_driver);
802 if (retval) {
803 pr_err("%s(%d): CHAR registration failed of pti driver\n",
804 __func__, __LINE__);
805 pr_err("%s(%d): Error value returned: %d\n",
806 __func__, __LINE__, retval);
807 goto err;
808 }
809
810 retval = pci_enable_device(pdev);
811 if (retval != 0) {
812 dev_err(&pdev->dev,
813 "%s: pci_enable_device() returned error %d\n",
814 __func__, retval);
815 goto err_unreg_misc;
816 }
817
818 drv_data = kzalloc(sizeof(*drv_data), GFP_KERNEL);
819 if (drv_data == NULL) {
820 retval = -ENOMEM;
821 dev_err(&pdev->dev,
822 "%s(%d): kmalloc() returned NULL memory.\n",
823 __func__, __LINE__);
824 goto err_disable_pci;
825 }
826 drv_data->pti_addr = pci_resource_start(pdev, pci_bar);
827
828 retval = pci_request_region(pdev, pci_bar, dev_name(&pdev->dev));
829 if (retval != 0) {
830 dev_err(&pdev->dev,
831 "%s(%d): pci_request_region() returned error %d\n",
832 __func__, __LINE__, retval);
833 goto err_free_dd;
834 }
835 drv_data->aperture_base = drv_data->pti_addr+APERTURE_14;
836 drv_data->pti_ioaddr =
837 ioremap_nocache((u32)drv_data->aperture_base,
838 APERTURE_LEN);
839 if (!drv_data->pti_ioaddr) {
840 retval = -ENOMEM;
841 goto err_rel_reg;
842 }
843
844 pci_set_drvdata(pdev, drv_data);
845
846 for (a = 0; a < PTITTY_MINOR_NUM; a++) {
847 struct tty_port *port = &drv_data->port[a];
848 tty_port_init(port);
849 port->ops = &tty_port_ops;
850
851 tty_port_register_device(port, pti_tty_driver, a, &pdev->dev);
852 }
853
854 register_console(&pti_console);
855
856 return 0;
857err_rel_reg:
858 pci_release_region(pdev, pci_bar);
859err_free_dd:
860 kfree(drv_data);
861err_disable_pci:
862 pci_disable_device(pdev);
863err_unreg_misc:
864 misc_deregister(&pti_char_driver);
865err:
866 return retval;
867}
868
869/**
870 * pti_pci_remove()- Driver exit method to remove PTI from
871 * PCI bus.
872 * @pdev: variable containing pci info of PTI.
873 */
874static void pti_pci_remove(struct pci_dev *pdev)
875{
876 struct pti_dev *drv_data = pci_get_drvdata(pdev);
877 unsigned int a;
878
879 unregister_console(&pti_console);
880
881 for (a = 0; a < PTITTY_MINOR_NUM; a++) {
882 tty_unregister_device(pti_tty_driver, a);
883 tty_port_destroy(&drv_data->port[a]);
884 }
885
886 iounmap(drv_data->pti_ioaddr);
887 kfree(drv_data);
888 pci_release_region(pdev, 1);
889 pci_disable_device(pdev);
890
891 misc_deregister(&pti_char_driver);
892}
893
894static struct pci_driver pti_pci_driver = {
895 .name = PCINAME,
896 .id_table = pci_ids,
897 .probe = pti_pci_probe,
898 .remove = pti_pci_remove,
899};
900
901/**
902 *
903 * pti_init()- Overall entry/init call to the pti driver.
904 * It starts the registration process with the kernel.
905 *
906 * Returns:
907 * int __init, 0 for success
908 * otherwise value is an error
909 *
910 */
911static int __init pti_init(void)
912{
913 int retval = -EINVAL;
914
915 /* First register module as tty device */
916
917 pti_tty_driver = alloc_tty_driver(PTITTY_MINOR_NUM);
918 if (pti_tty_driver == NULL) {
919 pr_err("%s(%d): Memory allocation failed for ptiTTY driver\n",
920 __func__, __LINE__);
921 return -ENOMEM;
922 }
923
924 pti_tty_driver->driver_name = DRIVERNAME;
925 pti_tty_driver->name = TTYNAME;
926 pti_tty_driver->major = 0;
927 pti_tty_driver->minor_start = PTITTY_MINOR_START;
928 pti_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
929 pti_tty_driver->subtype = SYSTEM_TYPE_SYSCONS;
930 pti_tty_driver->flags = TTY_DRIVER_REAL_RAW |
931 TTY_DRIVER_DYNAMIC_DEV;
932 pti_tty_driver->init_termios = tty_std_termios;
933
934 tty_set_operations(pti_tty_driver, &pti_tty_driver_ops);
935
936 retval = tty_register_driver(pti_tty_driver);
937 if (retval) {
938 pr_err("%s(%d): TTY registration failed of pti driver\n",
939 __func__, __LINE__);
940 pr_err("%s(%d): Error value returned: %d\n",
941 __func__, __LINE__, retval);
942
943 goto put_tty;
944 }
945
946 retval = pci_register_driver(&pti_pci_driver);
947 if (retval) {
948 pr_err("%s(%d): PCI registration failed of pti driver\n",
949 __func__, __LINE__);
950 pr_err("%s(%d): Error value returned: %d\n",
951 __func__, __LINE__, retval);
952 goto unreg_tty;
953 }
954
955 return 0;
956unreg_tty:
957 tty_unregister_driver(pti_tty_driver);
958put_tty:
959 put_tty_driver(pti_tty_driver);
960 pti_tty_driver = NULL;
961 return retval;
962}
963
964/**
965 * pti_exit()- Unregisters this module as a tty and pci driver.
966 */
967static void __exit pti_exit(void)
968{
969 tty_unregister_driver(pti_tty_driver);
970 pci_unregister_driver(&pti_pci_driver);
971 put_tty_driver(pti_tty_driver);
972}
973
974module_init(pti_init);
975module_exit(pti_exit);
976
977MODULE_LICENSE("GPL");
978MODULE_AUTHOR("Ken Mills, Jay Freyensee");
979MODULE_DESCRIPTION("PTI Driver");
980