blob: 8a1e0da6b82767759d3ebd8c406cee3294682fd6 [file] [log] [blame]
Joel Niderb9662ca2012-06-10 14:21:11 +03001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider5bd73f82011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider5bd73f82011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
28#include <linux/delay.h> /* msleep */
29#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020030#include <linux/clk.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020031#include <linux/poll.h> /* poll() file op */
32#include <linux/wait.h> /* wait() macros, sleeping */
33#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020034#include <linux/bitops.h> /* BIT() macro */
Joel Nider5bd73f82011-12-14 16:53:30 +020035#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020036#include <mach/gpio.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020037#include <linux/wakelock.h> /* Locking functions */
Joel Nider5556a852011-10-16 10:52:13 +020038#include <mach/dma.h>
39#include <mach/msm_tspp.h>
Joel Nider5556a852011-10-16 10:52:13 +020040#include <linux/debugfs.h>
Joel Nider5556a852011-10-16 10:52:13 +020041
42/*
43 * General defines
44 */
Joel Nider5556a852011-10-16 10:52:13 +020045#define TSPP_TSIF_INSTANCES 2
46#define TSPP_FILTER_TABLES 3
Joel Nider5bd73f82011-12-14 16:53:30 +020047#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020048#define TSPP_NUM_CHANNELS 16
49#define TSPP_NUM_PRIORITIES 16
50#define TSPP_NUM_KEYS 8
51#define INVALID_CHANNEL 0xFFFFFFFF
Joel Nider5bd73f82011-12-14 16:53:30 +020052#define TSPP_SPS_DESCRIPTOR_COUNT 128
Joel Nider5556a852011-10-16 10:52:13 +020053#define TSPP_PACKET_LENGTH 188
54#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Joel Nider5bd73f82011-12-14 16:53:30 +020055#define TSPP_MAX_BUFFER_SIZE (32 * 1024)
56#define TSPP_NUM_BUFFERS 64
Joel Nider5556a852011-10-16 10:52:13 +020057#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
58#define SPS_DESCRIPTOR_SIZE 8
59#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider5bd73f82011-12-14 16:53:30 +020060#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020061
62/*
63 * TSIF register offsets
64 */
65#define TSIF_STS_CTL_OFF (0x0)
66#define TSIF_TIME_LIMIT_OFF (0x4)
67#define TSIF_CLK_REF_OFF (0x8)
68#define TSIF_LPBK_FLAGS_OFF (0xc)
69#define TSIF_LPBK_DATA_OFF (0x10)
70#define TSIF_TEST_CTL_OFF (0x14)
71#define TSIF_TEST_MODE_OFF (0x18)
72#define TSIF_TEST_RESET_OFF (0x1c)
73#define TSIF_TEST_EXPORT_OFF (0x20)
74#define TSIF_TEST_CURRENT_OFF (0x24)
75
76#define TSIF_DATA_PORT_OFF (0x100)
77
78/* bits for TSIF_STS_CTL register */
79#define TSIF_STS_CTL_EN_IRQ BIT(28)
80#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
81#define TSIF_STS_CTL_1ST_PACKET BIT(26)
82#define TSIF_STS_CTL_OVERFLOW BIT(25)
83#define TSIF_STS_CTL_LOST_SYNC BIT(24)
84#define TSIF_STS_CTL_TIMEOUT BIT(23)
85#define TSIF_STS_CTL_INV_SYNC BIT(21)
86#define TSIF_STS_CTL_INV_NULL BIT(20)
87#define TSIF_STS_CTL_INV_ERROR BIT(19)
88#define TSIF_STS_CTL_INV_ENABLE BIT(18)
89#define TSIF_STS_CTL_INV_DATA BIT(17)
90#define TSIF_STS_CTL_INV_CLOCK BIT(16)
91#define TSIF_STS_CTL_SPARE BIT(15)
92#define TSIF_STS_CTL_EN_NULL BIT(11)
93#define TSIF_STS_CTL_EN_ERROR BIT(10)
94#define TSIF_STS_CTL_LAST_BIT BIT(9)
95#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
96#define TSIF_STS_CTL_EN_TCR BIT(7)
97#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider5bd73f82011-12-14 16:53:30 +020098#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +020099#define TSIF_STS_CTL_EN_DM BIT(4)
100#define TSIF_STS_CTL_STOP BIT(3)
101#define TSIF_STS_CTL_START BIT(0)
102
103/*
104 * TSPP register offsets
105 */
106#define TSPP_RST 0x00
107#define TSPP_CLK_CONTROL 0x04
108#define TSPP_CONFIG 0x08
109#define TSPP_CONTROL 0x0C
110#define TSPP_PS_DISABLE 0x10
111#define TSPP_MSG_IRQ_STATUS 0x14
112#define TSPP_MSG_IRQ_MASK 0x18
113#define TSPP_IRQ_STATUS 0x1C
114#define TSPP_IRQ_MASK 0x20
115#define TSPP_IRQ_CLEAR 0x24
116#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
117#define TSPP_STATUS 0x68
118#define TSPP_CURR_TSP_HEADER 0x6C
119#define TSPP_CURR_PID_FILTER 0x70
120#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
121#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
122#define TSPP_DATA_KEY_RESET 0x9C
123#define TSPP_KEY_VALID 0xA0
124#define TSPP_KEY_ERROR 0xA4
125#define TSPP_TEST_CTRL 0xA8
126#define TSPP_VERSION 0xAC
127#define TSPP_GENERICS 0xB0
128#define TSPP_NOP 0xB4
129
130/*
131 * Register bit definitions
132 */
133/* TSPP_RST */
134#define TSPP_RST_RESET BIT(0)
135
136/* TSPP_CLK_CONTROL */
137#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
138#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
139#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
140#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
141#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
142#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
143#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
144#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
145#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
146#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
147
148/* TSPP_CONFIG */
149#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
150((_b & 0xF) << 8))
151#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
152#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
153#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
154#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
155#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
156#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
157#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
158#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
159#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
160
161/* TSPP_CONTROL */
162#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
163#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
164#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
165#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
166#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
167#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
168
169/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
170#define TSPP_MSG_TSPP_IRQ BIT(2)
171#define TSPP_MSG_TSIF_1_IRQ BIT(1)
172#define TSPP_MSG_TSIF_0_IRQ BIT(0)
173
174/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
175#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
176#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
177#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
178#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
179#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
180
181/* TSPP_PIPE_ERROR_STATUS */
182#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
183#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
184#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
185#define TSPP_PIP_PS_LOST_START BIT(0)
186
187/* TSPP_STATUS */
188#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
189#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
190#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
191#define TSPP_CURR_FILTER_TABLE BIT(0)
192
193/* TSPP_GENERICS */
194#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
195#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
196#define TSPP_GENERICS_MAX_PIPES BIT(2)
197#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
198#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
199
200/*
201 * TSPP memory regions
202 */
203#define TSPP_PID_FILTER_TABLE0 0x800
204#define TSPP_PID_FILTER_TABLE1 0x880
205#define TSPP_PID_FILTER_TABLE2 0x900
206#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
207#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
208#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
209#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
210#define TSPP_DATA_KEY 0xCD0
211
Joel Nider5556a852011-10-16 10:52:13 +0200212struct debugfs_entry {
213 const char *name;
214 mode_t mode;
215 int offset;
216};
217
218static const struct debugfs_entry debugfs_tsif_regs[] = {
219 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
220 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
221 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
222 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
223 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
224 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
225 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
226 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
227 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
228 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
229 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
230};
231
232static const struct debugfs_entry debugfs_tspp_regs[] = {
233 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
234 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
235 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
236 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
237 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
238 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
239 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
240 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
241 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
242 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
243 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
244 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
245 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
246 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
247 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
248 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
249 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
250 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
251 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
252 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
253 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
254 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
255 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
256 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
257 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
258 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
259 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
260 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
261 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
262};
263
Joel Nider5556a852011-10-16 10:52:13 +0200264struct tspp_pid_filter {
265 u32 filter; /* see FILTER_ macros */
266 u32 config; /* see FILTER_ macros */
267};
268
269/* tsp_info */
270#define FILTER_HEADER_ERROR_MASK BIT(7)
271#define FILTER_TRANS_END_DISABLE BIT(6)
272#define FILTER_DEC_ON_ERROR_EN BIT(5)
273#define FILTER_DECRYPT BIT(4)
274#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
275#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
276#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
277 (_p->config & ~0xF) | (_b & 0xF))
278#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
279#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
280 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
281#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
282#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
283 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
284#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
285#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
286 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
287#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
288#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
289 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
290#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
291#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
292 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
293
294struct tspp_global_performance_regs {
295 u32 tsp_total;
296 u32 tsp_ignored;
297 u32 tsp_error;
298 u32 tsp_sync;
299};
300
301struct tspp_pipe_context_regs {
302 u16 pes_bytes_left;
303 u16 count;
304 u32 tsif_suffix;
305} __packed;
306#define CONTEXT_GET_STATE(_a) (_a & 0x3)
307#define CONTEXT_UNSPEC_LENGTH BIT(11)
308#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
309
310struct tspp_pipe_performance_regs {
311 u32 tsp_total;
312 u32 ps_duplicate_tsp;
313 u32 tsp_no_payload;
314 u32 tsp_broken_ps;
315 u32 ps_total_num;
316 u32 ps_continuity_error;
317 u32 ps_length_error;
318 u32 pes_sync_error;
319};
320
321struct tspp_tsif_device {
322 void __iomem *base;
323 u32 time_limit;
324 u32 ref_count;
Joel Nider5bd73f82011-12-14 16:53:30 +0200325 enum tspp_tsif_mode mode;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200326 int clock_inverse;
327 int data_inverse;
328 int sync_inverse;
329 int enable_inverse;
Joel Nider5556a852011-10-16 10:52:13 +0200330
331 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200332 struct dentry *dent_tsif;
333 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Joel Nider5556a852011-10-16 10:52:13 +0200334};
335
336enum tspp_buf_state {
337 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
338 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider5bd73f82011-12-14 16:53:30 +0200339 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
340 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200341};
342
343struct tspp_mem_buffer {
Joel Nider5bd73f82011-12-14 16:53:30 +0200344 struct tspp_mem_buffer *next;
345 struct sps_mem_buffer sps;
346 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200347 enum tspp_buf_state state;
348 size_t filled; /* how much data this buffer is holding */
349 int read_index; /* where to start reading data from */
350};
351
352/* this represents each char device 'channel' */
353struct tspp_channel {
354 struct cdev cdev;
355 struct device *dd;
Joel Nider5bd73f82011-12-14 16:53:30 +0200356 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200357 struct sps_pipe *pipe;
358 struct sps_connect config;
359 struct sps_register_event event;
Joel Nider5bd73f82011-12-14 16:53:30 +0200360 struct tspp_mem_buffer *data; /* list of buffers */
361 struct tspp_mem_buffer *read; /* first buffer ready to be read */
362 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
363 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200364 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider5bd73f82011-12-14 16:53:30 +0200365 u32 id; /* channel id (0-15) */
366 int used; /* is this channel in use? */
367 int key; /* which encryption key index is used */
368 u32 buffer_size; /* size of the sps transfer buffers */
369 u32 max_buffers; /* how many buffers should be allocated */
370 u32 buffer_count; /* how many buffers are actually allocated */
371 u32 filter_count; /* how many filters have been added to this channel */
372 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200373 enum tspp_source src;
374 enum tspp_mode mode;
Joel Nider5bd73f82011-12-14 16:53:30 +0200375 tspp_notifier *notifier; /* used only with kernel api */
376 void *notify_data; /* data to be passed with the notifier */
377 u32 notify_timer; /* notification for partially filled buffers */
Joel Nider5556a852011-10-16 10:52:13 +0200378};
379
380struct tspp_pid_filter_table {
381 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
382};
383
384struct tspp_key_entry {
385 u32 even_lsb;
386 u32 even_msb;
387 u32 odd_lsb;
388 u32 odd_msb;
389};
390
391struct tspp_key_table {
392 struct tspp_key_entry entry[TSPP_NUM_KEYS];
393};
394
Joel Nider5bd73f82011-12-14 16:53:30 +0200395/* this represents the actual hardware device */
396struct tspp_device {
397 struct list_head devlist; /* list of all devices */
398 struct platform_device *pdev;
399 void __iomem *base;
400 unsigned int tspp_irq;
401 unsigned int bam_irq;
402 u32 bam_handle;
403 struct sps_bam_props bam_props;
404 struct wake_lock wake_lock;
405 spinlock_t spinlock;
406 struct tasklet_struct tlet;
407 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
408 /* clocks */
409 struct clk *tsif_pclk;
410 struct clk *tsif_ref_clk;
411 /* data */
412 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
413 struct tspp_channel channels[TSPP_NUM_CHANNELS];
414 struct tspp_key_table *tspp_key_table;
415 struct tspp_global_performance_regs *tspp_global_performance;
416 struct tspp_pipe_context_regs *tspp_pipe_context;
417 struct tspp_pipe_performance_regs *tspp_pipe_performance;
418
419 struct dentry *dent;
420 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
421};
422
423
Joel Nider5556a852011-10-16 10:52:13 +0200424static struct class *tspp_class;
425static int tspp_key_entry;
426static dev_t tspp_minor; /* next minor number to assign */
Joel Nider5bd73f82011-12-14 16:53:30 +0200427
428static LIST_HEAD(tspp_devices);
429
430/* forward declarations */
431static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
432static ssize_t tspp_open(struct inode *inode, struct file *filp);
433static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
434static ssize_t tspp_release(struct inode *inode, struct file *filp);
435static long tspp_ioctl(struct file *, unsigned int, unsigned long);
436
437/* file operations */
438static const struct file_operations tspp_fops = {
439 .owner = THIS_MODULE,
440 .read = tspp_read,
441 .open = tspp_open,
442 .poll = tspp_poll,
443 .release = tspp_release,
444 .unlocked_ioctl = tspp_ioctl,
445};
Joel Nider5556a852011-10-16 10:52:13 +0200446
447/*** IRQ ***/
Joel Nider5bd73f82011-12-14 16:53:30 +0200448static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200449{
Joel Nider5bd73f82011-12-14 16:53:30 +0200450 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200451 u32 status, mask;
452 u32 data;
453
454 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
455 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
456 status &= mask;
457
458 if (!status) {
459 dev_warn(&device->pdev->dev, "Spurious interrupt");
460 return IRQ_NONE;
461 }
462
463 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
464
465 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
466 /* read the key error info */
467 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
468 dev_info(&device->pdev->dev, "key error 0x%x", data);
469 }
470 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
471 data = readl_relaxed(device->base + TSPP_KEY_VALID);
472 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
473 }
474 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
475 dev_info(&device->pdev->dev, "key switched");
476
477 if (status & 0xffff)
Joel Nider5bd73f82011-12-14 16:53:30 +0200478 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200479
480 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
481 wmb();
482 return IRQ_HANDLED;
483}
484
485/*** callbacks ***/
486static void tspp_sps_complete_cb(struct sps_event_notify *notify)
487{
Joel Nider5bd73f82011-12-14 16:53:30 +0200488 struct tspp_device *pdev = notify->user;
489 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200490}
491
492/*** tasklet ***/
493static void tspp_sps_complete_tlet(unsigned long data)
494{
495 int i;
496 int complete;
497 unsigned long flags;
498 struct sps_iovec iovec;
499 struct tspp_channel *channel;
500 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200501 spin_lock_irqsave(&device->spinlock, flags);
502
503 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
504 complete = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +0200505 channel = &device->channels[i];
506 if (!channel->used || !channel->waiting)
507 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200508
509 /* get completions */
Joel Nider5bd73f82011-12-14 16:53:30 +0200510 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200511 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
512 pr_err("tspp: Error in iovec on channel %i",
513 channel->id);
514 break;
515 }
516 if (iovec.size == 0)
517 break;
518
Joel Nider5bd73f82011-12-14 16:53:30 +0200519 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200520 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider5bd73f82011-12-14 16:53:30 +0200521 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200522
523 complete = 1;
Joel Nider5bd73f82011-12-14 16:53:30 +0200524 channel->waiting->state = TSPP_BUF_STATE_DATA;
525 channel->waiting->filled = iovec.size;
526 channel->waiting->read_index = 0;
527
528 /* update the pointers */
529 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200530 }
531
Joel Nider5bd73f82011-12-14 16:53:30 +0200532 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200533 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200534 wake_up_interruptible(&channel->in_queue);
Joel Nider5bd73f82011-12-14 16:53:30 +0200535
536 /* call notifiers */
537 if (channel->notifier)
538 channel->notifier(channel->id,
539 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200540 }
541 }
542
543 spin_unlock_irqrestore(&device->spinlock, flags);
544}
545
546/*** GPIO functions ***/
547static void tspp_gpios_free(const struct msm_gpio *table, int size)
548{
549 int i;
550 const struct msm_gpio *g;
551 for (i = size-1; i >= 0; i--) {
552 g = table + i;
553 gpio_free(GPIO_PIN(g->gpio_cfg));
554 }
555}
556
557static int tspp_gpios_request(const struct msm_gpio *table, int size)
558{
559 int rc;
560 int i;
561 const struct msm_gpio *g;
562 for (i = 0; i < size; i++) {
563 g = table + i;
564 rc = gpio_request(GPIO_PIN(g->gpio_cfg), g->label);
565 if (rc) {
566 pr_err("tspp: gpio_request(%d) <%s> failed: %d\n",
567 GPIO_PIN(g->gpio_cfg), g->label ?: "?", rc);
568 goto err;
569 }
570 }
571 return 0;
572err:
573 tspp_gpios_free(table, i);
574 return rc;
575}
576
577static int tspp_gpios_disable(const struct msm_gpio *table, int size)
578{
579 int rc = 0;
580 int i;
581 const struct msm_gpio *g;
582 for (i = size-1; i >= 0; i--) {
583 int tmp;
584 g = table + i;
585 tmp = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_DISABLE);
586 if (tmp) {
587 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE)"
588 " <%s> failed: %d\n",
589 g->gpio_cfg, g->label ?: "?", rc);
590 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
591 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
592 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
593 GPIO_DRVSTR(g->gpio_cfg));
594 if (!rc)
595 rc = tmp;
596 }
597 }
598
599 return rc;
600}
601
602static int tspp_gpios_enable(const struct msm_gpio *table, int size)
603{
604 int rc;
605 int i;
606 const struct msm_gpio *g;
607 for (i = 0; i < size; i++) {
608 g = table + i;
609 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
610 if (rc) {
611 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE)"
612 " <%s> failed: %d\n",
613 g->gpio_cfg, g->label ?: "?", rc);
614 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
615 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
616 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
617 GPIO_DRVSTR(g->gpio_cfg));
618 goto err;
619 }
620 }
621 return 0;
622err:
623 tspp_gpios_disable(table, i);
624 return rc;
625}
626
627static int tspp_gpios_request_enable(const struct msm_gpio *table, int size)
628{
629 int rc = tspp_gpios_request(table, size);
630 if (rc)
631 return rc;
632 rc = tspp_gpios_enable(table, size);
633 if (rc)
634 tspp_gpios_free(table, size);
635 return rc;
636}
637
638static void tspp_gpios_disable_free(const struct msm_gpio *table, int size)
639{
640 tspp_gpios_disable(table, size);
641 tspp_gpios_free(table, size);
642}
643
644static int tspp_start_gpios(struct tspp_device *device)
645{
646 struct msm_tspp_platform_data *pdata =
647 device->pdev->dev.platform_data;
648 return tspp_gpios_request_enable(pdata->gpios, pdata->num_gpios);
649}
650
651static void tspp_stop_gpios(struct tspp_device *device)
652{
653 struct msm_tspp_platform_data *pdata =
654 device->pdev->dev.platform_data;
655 tspp_gpios_disable_free(pdata->gpios, pdata->num_gpios);
656}
657
Joel Nider5bd73f82011-12-14 16:53:30 +0200658/*** Clock functions ***/
659static int tspp_clock_start(struct tspp_device *device)
660{
661 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
662 pr_err("tspp: Can't start pclk");
663 return -EBUSY;
664 }
665
666 if (device->tsif_ref_clk &&
667 clk_prepare_enable(device->tsif_ref_clk) != 0) {
668 pr_err("tspp: Can't start ref clk");
669 clk_disable_unprepare(device->tsif_pclk);
670 return -EBUSY;
671 }
672
673 return 0;
674}
675
676static void tspp_clock_stop(struct tspp_device *device)
677{
678 if (device->tsif_pclk)
679 clk_disable(device->tsif_pclk);
680
681 if (device->tsif_ref_clk)
682 clk_disable(device->tsif_ref_clk);
683}
684
Joel Nider5556a852011-10-16 10:52:13 +0200685/*** TSIF functions ***/
686static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
687{
688 int start_hardware = 0;
689 u32 ctl;
690
691 if (tsif_device->ref_count == 0) {
692 start_hardware = 1;
693 } else if (tsif_device->ref_count > 0) {
694 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
695 if ((ctl & TSIF_STS_CTL_START) != 1) {
696 /* this hardware should already be running */
697 pr_warn("tspp: tsif hw not started but ref count > 0");
698 start_hardware = 1;
699 }
700 }
701
702 if (start_hardware) {
Joel Nider5bd73f82011-12-14 16:53:30 +0200703 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200704 TSIF_STS_CTL_EN_DM;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200705
706 if (tsif_device->clock_inverse)
707 ctl |= TSIF_STS_CTL_INV_CLOCK;
708
709 if (tsif_device->data_inverse)
710 ctl |= TSIF_STS_CTL_INV_DATA;
711
712 if (tsif_device->sync_inverse)
713 ctl |= TSIF_STS_CTL_INV_SYNC;
714
715 if (tsif_device->enable_inverse)
716 ctl |= TSIF_STS_CTL_INV_ENABLE;
717
Joel Nider5bd73f82011-12-14 16:53:30 +0200718 switch (tsif_device->mode) {
719 case TSPP_TSIF_MODE_LOOPBACK:
720 ctl |= TSIF_STS_CTL_EN_NULL |
721 TSIF_STS_CTL_EN_ERROR |
722 TSIF_STS_CTL_TEST_MODE;
723 break;
724 case TSPP_TSIF_MODE_1:
725 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
726 TSIF_STS_CTL_EN_TCR;
727 break;
728 case TSPP_TSIF_MODE_2:
729 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
730 TSIF_STS_CTL_EN_TCR |
731 TSIF_STS_CTL_MODE_2;
732 break;
733 default:
734 pr_warn("tspp: unknown tsif mode 0x%x",
735 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200736 }
737 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
738 writel_relaxed(tsif_device->time_limit,
739 tsif_device->base + TSIF_TIME_LIMIT_OFF);
740 wmb();
741 writel_relaxed(ctl | TSIF_STS_CTL_START,
742 tsif_device->base + TSIF_STS_CTL_OFF);
743 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200744 }
745
Joel Nider5bd73f82011-12-14 16:53:30 +0200746 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200747 tsif_device->ref_count++;
748
Joel Nider5bd73f82011-12-14 16:53:30 +0200749 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200750}
751
752static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
753{
754 if (tsif_device->ref_count == 0)
755 return;
756
757 tsif_device->ref_count--;
758
759 if (tsif_device->ref_count == 0) {
760 writel_relaxed(TSIF_STS_CTL_STOP,
761 tsif_device->base + TSIF_STS_CTL_OFF);
762 wmb();
763 }
764}
765
Joel Nider5bd73f82011-12-14 16:53:30 +0200766/*** local TSPP functions ***/
767static int tspp_channels_in_use(struct tspp_device *pdev)
768{
769 int i;
770 int count = 0;
771 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
772 count += (pdev->channels[i].used ? 1 : 0);
773
774 return count;
775}
776
777static struct tspp_device *tspp_find_by_id(int id)
778{
779 struct tspp_device *dev;
780 list_for_each_entry(dev, &tspp_devices, devlist) {
781 if (dev->pdev->id == id)
782 return dev;
783 }
784 return NULL;
785}
786
Joel Nider5556a852011-10-16 10:52:13 +0200787static int tspp_get_key_entry(void)
788{
789 int i;
790 for (i = 0; i < TSPP_NUM_KEYS; i++) {
791 if (!(tspp_key_entry & (1 << i))) {
792 tspp_key_entry |= (1 << i);
793 return i;
794 }
795 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200796 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200797}
798
799static void tspp_free_key_entry(int entry)
800{
801 if (entry > TSPP_NUM_KEYS) {
802 pr_err("tspp_free_key_entry: index out of bounds");
803 return;
804 }
805
806 tspp_key_entry &= ~(1 << entry);
807}
808
Joel Nider5bd73f82011-12-14 16:53:30 +0200809static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
810 u32 size, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200811{
Joel Nider5bd73f82011-12-14 16:53:30 +0200812 if (size < TSPP_MIN_BUFFER_SIZE ||
813 size > TSPP_MAX_BUFFER_SIZE) {
814 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200815 return -ENOMEM;
816 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200817
818 if (alloc) {
819 TSPP_DEBUG("tspp using alloc function");
820 desc->virt_base = alloc(channel_id, size,
821 &desc->phys_base, user);
822 } else {
823 desc->virt_base = dma_alloc_coherent(NULL, size,
824 &desc->phys_base, GFP_KERNEL);
825 if (desc->virt_base == 0) {
826 pr_err("tspp dma alloc coherent failed %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200827 return -ENOMEM;
828 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200829 }
830
831 desc->size = size;
832 return 0;
833}
834
835static int tspp_queue_buffer(struct tspp_channel *channel,
836 struct tspp_mem_buffer *buffer)
837{
838 int rc;
839 u32 flags = 0;
840
841 /* make sure the interrupt frequency is valid */
842 if (channel->int_freq < 1)
843 channel->int_freq = 1;
844
845 /* generate interrupt according to requested frequency */
846 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
847 flags = SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOB;
848
849 /* start the transfer */
850 rc = sps_transfer_one(channel->pipe,
851 buffer->sps.phys_base,
852 buffer->sps.size,
853 channel->pdev,
854 flags);
855 if (rc < 0)
856 return rc;
857
858 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200859
860 return 0;
861}
862
863static int tspp_global_reset(struct tspp_device *pdev)
864{
865 u32 i, val;
866
867 /* stop all TSIFs */
868 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
869 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
870 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
871 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200872 pdev->tsif[i].clock_inverse = 0;
873 pdev->tsif[i].data_inverse = 0;
874 pdev->tsif[i].sync_inverse = 0;
875 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200876 }
877 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
878 wmb();
879
880 /* BAM */
881 if (sps_device_reset(pdev->bam_handle) != 0) {
882 pr_err("tspp: error resetting bam");
Joel Nider5bd73f82011-12-14 16:53:30 +0200883 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200884 }
885
886 /* TSPP tables */
887 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider5bd73f82011-12-14 16:53:30 +0200888 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +0200889 0, sizeof(struct tspp_pid_filter_table));
890
891 /* disable all filters */
892 val = (2 << TSPP_NUM_CHANNELS) - 1;
893 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
894
895 /* TSPP registers */
896 val = readl_relaxed(pdev->base + TSPP_CONTROL);
897 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
898 pdev->base + TSPP_CONTROL);
899 wmb();
Joel Nider5bd73f82011-12-14 16:53:30 +0200900 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200901 sizeof(struct tspp_global_performance_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +0200902 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200903 sizeof(struct tspp_pipe_context_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +0200904 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200905 sizeof(struct tspp_pipe_performance_regs));
906 wmb();
907 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
908 pdev->base + TSPP_CONTROL);
909 wmb();
910
911 val = readl_relaxed(pdev->base + TSPP_CONFIG);
912 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
913 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
914 TSPP_CONFIG_PS_CONT_ERR_MASK);
915 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
916 writel_relaxed(val, pdev->base + TSPP_CONFIG);
917 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_MASK);
918 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
919 writel_relaxed(0, pdev->base + TSPP_RST);
920 wmb();
921
922 tspp_key_entry = 0;
923
924 return 0;
925}
926
Joel Nider5bd73f82011-12-14 16:53:30 +0200927static int tspp_select_source(u32 dev, u32 channel_id,
928 struct tspp_select_source *src)
929{
930 /* make sure the requested src id is in bounds */
931 if (src->source > TSPP_SOURCE_MEM) {
932 pr_err("tspp source out of bounds");
933 return -EINVAL;
934 }
935
936 /* open the stream */
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200937 tspp_open_stream(dev, channel_id, src);
Joel Nider5bd73f82011-12-14 16:53:30 +0200938
939 return 0;
940}
941
942static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
943{
944 struct tspp_device *pdev = channel->pdev;
945
946 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
947 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
948 return 0;
949}
950
951static int tspp_set_system_keys(struct tspp_channel *channel,
952 struct tspp_system_keys *keys)
953{
954 int i;
955 struct tspp_device *pdev = channel->pdev;
956
957 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
958 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
959
960 return 0;
961}
962
963static int tspp_channel_init(struct tspp_channel *channel,
964 struct tspp_device *pdev)
965{
966 channel->cdev.owner = THIS_MODULE;
967 cdev_init(&channel->cdev, &tspp_fops);
968 channel->pdev = pdev;
969 channel->data = NULL;
970 channel->read = NULL;
971 channel->waiting = NULL;
972 channel->locked = NULL;
973 channel->id = MINOR(tspp_minor);
974 channel->used = 0;
975 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
976 channel->max_buffers = TSPP_NUM_BUFFERS;
977 channel->buffer_count = 0;
978 channel->filter_count = 0;
979 channel->int_freq = 1;
980 channel->notifier = NULL;
981 channel->notify_data = NULL;
982 channel->notify_timer = 0;
983 init_waitqueue_head(&channel->in_queue);
984
985 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
986 pr_err("tspp: cdev_add failed");
987 return -EBUSY;
988 }
989
990 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
991 channel, "tspp%02d", channel->id);
992 if (IS_ERR(channel->dd)) {
993 pr_err("tspp: device_create failed: %i",
994 (int)PTR_ERR(channel->dd));
995 cdev_del(&channel->cdev);
996 return -EBUSY;
997 }
998
999 return 0;
1000}
1001
1002static int tspp_set_buffer_size(struct tspp_channel *channel,
1003 struct tspp_buffer *buf)
1004{
1005 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1006 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1007 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1008 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1009 else
1010 channel->buffer_size = buf->size;
1011
1012 return 0;
1013}
1014
1015static void tspp_set_tsif_mode(struct tspp_channel *channel,
1016 enum tspp_tsif_mode mode)
1017{
1018 int index;
1019
1020 switch (channel->src) {
1021 case TSPP_SOURCE_TSIF0:
1022 index = 0;
1023 break;
1024 case TSPP_SOURCE_TSIF1:
1025 index = 1;
1026 break;
1027 default:
1028 pr_warn("tspp: can't set mode for non-tsif source %d",
1029 channel->src);
1030 return;
1031 }
1032 channel->pdev->tsif[index].mode = mode;
1033}
1034
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001035static void tspp_set_signal_inversion(struct tspp_channel *channel,
1036 int clock_inverse, int data_inverse,
1037 int sync_inverse, int enable_inverse)
1038{
1039 int index;
1040
1041 switch (channel->src) {
1042 case TSPP_SOURCE_TSIF0:
1043 index = 0;
1044 break;
1045 case TSPP_SOURCE_TSIF1:
1046 index = 1;
1047 break;
1048 default:
1049 return;
1050 }
1051 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1052 channel->pdev->tsif[index].data_inverse = data_inverse;
1053 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1054 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1055}
1056
Joel Nider5bd73f82011-12-14 16:53:30 +02001057/*** TSPP API functions ***/
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001058int tspp_open_stream(u32 dev, u32 channel_id, struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001059{
1060 u32 val;
1061 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001062 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001063
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001064 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1065 dev, channel_id, source->source, source->mode);
Joel Nider5bd73f82011-12-14 16:53:30 +02001066 if (dev >= TSPP_MAX_DEVICES) {
1067 pr_err("tspp: device id out of range");
1068 return -ENODEV;
1069 }
Joel Nider5556a852011-10-16 10:52:13 +02001070
Joel Nider5bd73f82011-12-14 16:53:30 +02001071 if (channel_id >= TSPP_NUM_CHANNELS) {
1072 pr_err("tspp: channel id out of range");
1073 return -ECHRNG;
1074 }
1075
1076 pdev = tspp_find_by_id(dev);
1077 if (!pdev) {
1078 pr_err("tspp_str: can't find device %i", dev);
1079 return -ENODEV;
1080 }
1081 channel = &pdev->channels[channel_id];
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001082 channel->src = source->source;
1083 tspp_set_tsif_mode(channel, source->mode);
1084 tspp_set_signal_inversion(channel, source->clk_inverse,
1085 source->data_inverse, source->sync_inverse,
1086 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001087
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001088 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001089 case TSPP_SOURCE_TSIF0:
1090 /* make sure TSIF0 is running & enabled */
1091 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1092 pr_err("tspp: error starting tsif0");
Joel Nider5bd73f82011-12-14 16:53:30 +02001093 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001094 }
1095 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1096 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1097 pdev->base + TSPP_CONTROL);
1098 wmb();
1099 break;
1100 case TSPP_SOURCE_TSIF1:
1101 /* make sure TSIF1 is running & enabled */
1102 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1103 pr_err("tspp: error starting tsif1");
Joel Nider5bd73f82011-12-14 16:53:30 +02001104 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001105 }
1106 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1107 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1108 pdev->base + TSPP_CONTROL);
1109 wmb();
1110 break;
1111 case TSPP_SOURCE_MEM:
1112 break;
1113 default:
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001114 pr_err("tspp: channel %i invalid source %i",
1115 channel->id, source->source);
Joel Nider5bd73f82011-12-14 16:53:30 +02001116 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001117 }
1118
Joel Nider5556a852011-10-16 10:52:13 +02001119 return 0;
1120}
1121EXPORT_SYMBOL(tspp_open_stream);
1122
Joel Nider5bd73f82011-12-14 16:53:30 +02001123int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001124{
1125 u32 val;
1126 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001127 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001128
Joel Nider5bd73f82011-12-14 16:53:30 +02001129 if (channel_id >= TSPP_NUM_CHANNELS) {
1130 pr_err("tspp: channel id out of range");
1131 return -ECHRNG;
1132 }
1133 pdev = tspp_find_by_id(dev);
1134 if (!pdev) {
1135 pr_err("tspp_cs: can't find device %i", dev);
1136 return -EBUSY;
1137 }
1138 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001139
1140 switch (channel->src) {
1141 case TSPP_SOURCE_TSIF0:
1142 tspp_stop_tsif(&pdev->tsif[0]);
1143 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1144 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1145 pdev->base + TSPP_CONTROL);
1146 wmb();
1147 break;
1148 case TSPP_SOURCE_TSIF1:
1149 tspp_stop_tsif(&pdev->tsif[1]);
1150 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1151 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1152 pdev->base + TSPP_CONTROL);
1153 break;
1154 case TSPP_SOURCE_MEM:
1155 break;
1156 case TSPP_SOURCE_NONE:
1157 break;
1158 }
1159
Joel Nider5bd73f82011-12-14 16:53:30 +02001160 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001161 return 0;
1162}
1163EXPORT_SYMBOL(tspp_close_stream);
1164
Joel Nider5bd73f82011-12-14 16:53:30 +02001165int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001166{
1167 int rc = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001168 struct sps_connect *config;
1169 struct sps_register_event *event;
1170 struct tspp_channel *channel;
1171 struct tspp_device *pdev;
1172
1173 if (channel_id >= TSPP_NUM_CHANNELS) {
1174 pr_err("tspp: channel id out of range");
1175 return -ECHRNG;
1176 }
1177 pdev = tspp_find_by_id(dev);
1178 if (!pdev) {
1179 pr_err("tspp_oc: can't find device %i", dev);
1180 return -ENODEV;
1181 }
1182 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001183
1184 if (channel->used) {
1185 pr_err("tspp channel already in use");
Joel Nider5bd73f82011-12-14 16:53:30 +02001186 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001187 }
1188
Joel Nider5bd73f82011-12-14 16:53:30 +02001189 config = &channel->config;
1190 event = &channel->event;
1191
1192 /* start the clocks if needed */
1193 tspp_clock_start(pdev);
1194 if (tspp_channels_in_use(pdev) == 0)
1195 wake_lock(&pdev->wake_lock);
1196
Joel Nider5556a852011-10-16 10:52:13 +02001197 /* mark it as used */
1198 channel->used = 1;
1199
1200 /* start the bam */
1201 channel->pipe = sps_alloc_endpoint();
1202 if (channel->pipe == 0) {
1203 pr_err("tspp: error allocating endpoint");
1204 rc = -ENOMEM;
1205 goto err_sps_alloc;
1206 }
1207
1208 /* get default configuration */
1209 sps_get_config(channel->pipe, config);
1210
Joel Nider5bd73f82011-12-14 16:53:30 +02001211 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001212 config->destination = SPS_DEV_HANDLE_MEM;
1213 config->mode = SPS_MODE_SRC;
Joel Nider5bd73f82011-12-14 16:53:30 +02001214 config->options =
1215 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1216 SPS_O_STREAMING | /* streaming mode */
1217 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
1218 SPS_O_ACK_TRANSFERS; /* must use sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001219 config->src_pipe_index = channel->id;
1220 config->desc.size =
1221 (TSPP_SPS_DESCRIPTOR_COUNT + 1) * SPS_DESCRIPTOR_SIZE;
1222 config->desc.base = dma_alloc_coherent(NULL,
1223 config->desc.size,
1224 &config->desc.phys_base,
1225 GFP_KERNEL);
1226 if (config->desc.base == 0) {
1227 pr_err("tspp: error allocating sps descriptors");
1228 rc = -ENOMEM;
1229 goto err_desc_alloc;
1230 }
1231
1232 memset(config->desc.base, 0, config->desc.size);
1233
1234 rc = sps_connect(channel->pipe, config);
1235 if (rc) {
1236 pr_err("tspp: error connecting bam");
1237 goto err_connect;
1238 }
1239
1240 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider5bd73f82011-12-14 16:53:30 +02001241 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001242 event->callback = tspp_sps_complete_cb;
1243 event->xfer_done = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001244 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001245
1246 rc = sps_register_event(channel->pipe, event);
1247 if (rc) {
1248 pr_err("tspp: error registering event");
1249 goto err_event;
1250 }
1251
Joel Nider5bd73f82011-12-14 16:53:30 +02001252 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001253 if (rc < 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001254 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001255 "Runtime PM: Unable to wake up tspp device, rc = %d",
1256 rc);
1257 }
Joel Nider5556a852011-10-16 10:52:13 +02001258 return 0;
1259
1260err_event:
1261 sps_disconnect(channel->pipe);
1262err_connect:
1263 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1264 config->desc.phys_base);
1265err_desc_alloc:
1266 sps_free_endpoint(channel->pipe);
1267err_sps_alloc:
1268 return rc;
1269}
1270EXPORT_SYMBOL(tspp_open_channel);
1271
Joel Nider5bd73f82011-12-14 16:53:30 +02001272int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001273{
1274 int i;
1275 int id;
1276 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001277
Joel Nider5bd73f82011-12-14 16:53:30 +02001278 struct sps_connect *config;
1279 struct tspp_device *pdev;
1280 struct tspp_channel *channel;
1281 struct tspp_mem_buffer *pbuf, *temp;
1282
1283 if (channel_id >= TSPP_NUM_CHANNELS) {
1284 pr_err("tspp: channel id out of range");
1285 return -ECHRNG;
1286 }
1287 pdev = tspp_find_by_id(dev);
1288 if (!pdev) {
1289 pr_err("tspp_close: can't find device %i", dev);
1290 return -ENODEV;
1291 }
1292 channel = &pdev->channels[channel_id];
1293
1294 /* if the channel is not used, we are done */
1295 if (!channel->used)
1296 return 0;
1297
1298 channel->notifier = NULL;
1299 channel->notify_data = NULL;
1300 channel->notify_timer = 0;
1301
1302 config = &channel->config;
1303 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001304
1305 /* disable pipe (channel) */
1306 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1307 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1308 wmb();
1309
1310 /* unregister all filters for this channel */
1311 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1312 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001313 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001314 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1315 if (id == channel->id) {
1316 if (FILTER_HAS_ENCRYPTION(tspp_filter))
1317 tspp_free_key_entry(
1318 FILTER_GET_KEY_NUMBER(tspp_filter));
1319 tspp_filter->config = 0;
1320 tspp_filter->filter = 0;
1321 }
1322 }
1323 channel->filter_count = 0;
1324
1325 /* stop the stream */
Joel Nider5bd73f82011-12-14 16:53:30 +02001326 tspp_close_stream(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02001327
1328 /* disconnect the bam */
1329 if (sps_disconnect(channel->pipe) != 0)
1330 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1331
1332 /* destroy the buffers */
1333 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1334 config->desc.phys_base);
1335
Joel Nider5bd73f82011-12-14 16:53:30 +02001336 pbuf = channel->data;
1337 for (i = 0; i < channel->buffer_count; i++) {
1338 if (pbuf->desc.phys_base) {
Joel Nider5556a852011-10-16 10:52:13 +02001339 dma_free_coherent(NULL,
Joel Nider5bd73f82011-12-14 16:53:30 +02001340 pbuf->desc.size,
1341 pbuf->desc.virt_base,
1342 pbuf->desc.phys_base);
1343 pbuf->desc.phys_base = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001344 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001345 pbuf->desc.virt_base = 0;
1346 pbuf->state = TSPP_BUF_STATE_EMPTY;
1347 temp = pbuf;
1348 pbuf = pbuf->next;
1349 kfree(temp);
Joel Nider5556a852011-10-16 10:52:13 +02001350 }
1351 channel->buffer_count = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001352 channel->data = NULL;
1353 channel->read = NULL;
1354 channel->waiting = NULL;
1355 channel->locked = NULL;
1356 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001357
Joel Nider5bd73f82011-12-14 16:53:30 +02001358 if (tspp_channels_in_use(pdev) == 0)
1359 wake_unlock(&pdev->wake_lock);
1360 tspp_clock_stop(pdev);
1361
Joel Nider5556a852011-10-16 10:52:13 +02001362 return 0;
1363}
1364EXPORT_SYMBOL(tspp_close_channel);
1365
Joel Nider5bd73f82011-12-14 16:53:30 +02001366int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001367 struct tspp_filter *filter)
1368{
1369 int i;
1370 int other_channel;
1371 int entry;
1372 u32 val, pid, enabled;
Joel Nider5bd73f82011-12-14 16:53:30 +02001373 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001374 struct tspp_pid_filter p;
Joel Nider5bd73f82011-12-14 16:53:30 +02001375 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001376
Joel Nider5bd73f82011-12-14 16:53:30 +02001377 TSPP_DEBUG("tspp: add filter");
1378 if (channel_id >= TSPP_NUM_CHANNELS) {
1379 pr_err("tspp: channel id out of range");
1380 return -ECHRNG;
1381 }
1382 pdev = tspp_find_by_id(dev);
1383 if (!pdev) {
1384 pr_err("tspp_add: can't find device %i", dev);
1385 return -ENODEV;
1386 }
1387
1388 channel = &pdev->channels[channel_id];
1389
Joel Nider5556a852011-10-16 10:52:13 +02001390 if (filter->source > TSPP_SOURCE_MEM) {
1391 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001392 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001393 }
1394
1395 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1396 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001397 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001398 }
1399
1400 /* make sure this filter mode matches the channel mode */
1401 switch (channel->mode) {
1402 case TSPP_MODE_DISABLED:
1403 channel->mode = filter->mode;
1404 break;
1405 case TSPP_MODE_RAW:
1406 case TSPP_MODE_PES:
1407 case TSPP_MODE_RAW_NO_SUFFIX:
1408 if (filter->mode != channel->mode) {
1409 pr_err("tspp: wrong filter mode");
Joel Nider5bd73f82011-12-14 16:53:30 +02001410 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001411 }
1412 }
1413
1414 if (filter->mode == TSPP_MODE_PES) {
1415 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1416 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001417 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001418 pid = FILTER_GET_PIPE_PID((tspp_filter));
1419 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1420 if (enabled && (pid == filter->pid)) {
1421 other_channel =
1422 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1423 pr_err("tspp: pid 0x%x already in use by channel %i",
1424 filter->pid, other_channel);
Joel Nider5bd73f82011-12-14 16:53:30 +02001425 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001426 }
1427 }
1428 }
1429
1430 /* make sure this priority is not already in use */
1431 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider5bd73f82011-12-14 16:53:30 +02001432 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001433 if (enabled) {
1434 pr_err("tspp: filter priority %i source %i is already enabled\n",
1435 filter->priority, channel->src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001436 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001437 }
1438
1439 if (channel->mode == TSPP_MODE_PES) {
1440 /* if we are already processing in PES mode, disable pipe
1441 (channel) and filter to be updated */
1442 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1443 writel_relaxed(val | (1 << channel->id),
1444 pdev->base + TSPP_PS_DISABLE);
1445 wmb();
1446 }
1447
1448 /* update entry */
1449 p.filter = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001450 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001451 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1452 FILTER_SET_PIPE_PID((&p), filter->pid);
1453 FILTER_SET_PID_MASK((&p), filter->mask);
1454 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1455 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1456 if (filter->decrypt) {
1457 entry = tspp_get_key_entry();
1458 if (entry == -1) {
1459 pr_err("tspp: no more keys available!");
1460 } else {
1461 p.config |= FILTER_DECRYPT;
1462 FILTER_SET_KEY_NUMBER((&p), entry);
1463 }
1464 }
Joel Nider5556a852011-10-16 10:52:13 +02001465
Joel Nider5bd73f82011-12-14 16:53:30 +02001466 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001467 filter[filter->priority].config = p.config;
Joel Nider5bd73f82011-12-14 16:53:30 +02001468 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001469 filter[filter->priority].filter = p.filter;
1470
Joel Nider5bd73f82011-12-14 16:53:30 +02001471 /* allocate buffers if needed */
1472 tspp_allocate_buffers(dev, channel->id, channel->max_buffers,
1473 channel->buffer_size, channel->int_freq, 0, 0);
1474 if (channel->buffer_count < MIN_ACCEPTABLE_BUFFER_COUNT) {
1475 pr_err("tspp: failed to allocate at least %i buffers",
1476 MIN_ACCEPTABLE_BUFFER_COUNT);
1477 return -ENOMEM;
1478 }
1479
Joel Nider5556a852011-10-16 10:52:13 +02001480 /* reenable pipe */
1481 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1482 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1483 wmb();
1484 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1485
Joel Nider5556a852011-10-16 10:52:13 +02001486 channel->filter_count++;
1487
1488 return 0;
1489}
1490EXPORT_SYMBOL(tspp_add_filter);
1491
Joel Nider5bd73f82011-12-14 16:53:30 +02001492int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001493 struct tspp_filter *filter)
1494{
1495 int entry;
1496 u32 val;
Joel Nider5bd73f82011-12-14 16:53:30 +02001497 struct tspp_device *pdev;
1498 int src;
1499 struct tspp_pid_filter *tspp_filter;
1500 struct tspp_channel *channel;
1501
1502 if (channel_id >= TSPP_NUM_CHANNELS) {
1503 pr_err("tspp: channel id out of range");
1504 return -ECHRNG;
1505 }
1506 pdev = tspp_find_by_id(dev);
1507 if (!pdev) {
1508 pr_err("tspp_remove: can't find device %i", dev);
1509 return -ENODEV;
1510 }
1511 channel = &pdev->channels[channel_id];
1512
1513 src = channel->src;
1514 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001515
1516 /* disable pipe (channel) */
1517 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1518 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1519 wmb();
1520
1521 /* update data keys */
1522 if (tspp_filter->config & FILTER_DECRYPT) {
1523 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1524 tspp_free_key_entry(entry);
1525 }
1526
1527 /* update pid table */
1528 tspp_filter->config = 0;
1529 tspp_filter->filter = 0;
1530
1531 channel->filter_count--;
1532
1533 /* reenable pipe */
1534 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1535 writel_relaxed(val & ~(1 << channel->id),
1536 pdev->base + TSPP_PS_DISABLE);
1537 wmb();
1538 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1539
1540 return 0;
1541}
1542EXPORT_SYMBOL(tspp_remove_filter);
1543
Joel Nider5bd73f82011-12-14 16:53:30 +02001544int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001545{
1546 int i;
1547 int id;
1548 int key_index;
1549 int data;
Joel Nider5bd73f82011-12-14 16:53:30 +02001550 struct tspp_channel *channel;
1551 struct tspp_device *pdev;
1552
1553 if (channel_id >= TSPP_NUM_CHANNELS) {
1554 pr_err("tspp: channel id out of range");
1555 return -ECHRNG;
1556 }
1557 pdev = tspp_find_by_id(dev);
1558 if (!pdev) {
1559 pr_err("tspp_set: can't find device %i", dev);
1560 return -ENODEV;
1561 }
1562 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001563
1564 /* read the key index used by this channel */
1565 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1566 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001567 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001568 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1569 if (id == channel->id) {
1570 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1571 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1572 break;
1573 }
1574 }
1575 }
1576 if (i == TSPP_NUM_PRIORITIES) {
1577 pr_err("tspp: no encryption on this channel");
Joel Nider5bd73f82011-12-14 16:53:30 +02001578 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001579 }
1580
1581 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001582 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1583 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001584 } else {
Joel Nider5bd73f82011-12-14 16:53:30 +02001585 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1586 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001587 }
1588 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1589
1590 return 0;
1591}
1592EXPORT_SYMBOL(tspp_set_key);
1593
Joel Nider5bd73f82011-12-14 16:53:30 +02001594int tspp_register_notification(u32 dev, u32 channel_id,
1595 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001596{
Joel Nider5bd73f82011-12-14 16:53:30 +02001597 struct tspp_channel *channel;
1598 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001599
Joel Nider5bd73f82011-12-14 16:53:30 +02001600 if (channel_id >= TSPP_NUM_CHANNELS) {
1601 pr_err("tspp: channel id out of range");
1602 return -ECHRNG;
1603 }
1604 pdev = tspp_find_by_id(dev);
1605 if (!pdev) {
1606 pr_err("tspp_reg: can't find device %i", dev);
1607 return -ENODEV;
1608 }
1609 channel = &pdev->channels[channel_id];
1610 channel->notifier = pNotify;
1611 channel->notify_data = userdata;
1612 channel->notify_timer = timer_ms;
Joel Nider5556a852011-10-16 10:52:13 +02001613 return 0;
1614}
Joel Nider5bd73f82011-12-14 16:53:30 +02001615EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001616
Joel Nider5bd73f82011-12-14 16:53:30 +02001617int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001618{
Joel Nider5bd73f82011-12-14 16:53:30 +02001619 struct tspp_channel *channel;
1620 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001621
Joel Nider5bd73f82011-12-14 16:53:30 +02001622 if (channel_id >= TSPP_NUM_CHANNELS) {
1623 pr_err("tspp: channel id out of range");
1624 return -ECHRNG;
1625 }
1626 pdev = tspp_find_by_id(dev);
1627 if (!pdev) {
1628 pr_err("tspp_unreg: can't find device %i", dev);
1629 return -ENODEV;
1630 }
1631 channel = &pdev->channels[channel_id];
1632 channel->notifier = NULL;
1633 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001634 return 0;
1635}
Joel Nider5bd73f82011-12-14 16:53:30 +02001636EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001637
Joel Nider5bd73f82011-12-14 16:53:30 +02001638const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001639{
Joel Nider5bd73f82011-12-14 16:53:30 +02001640 struct tspp_mem_buffer *buffer;
1641 struct tspp_channel *channel;
1642 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001643
Joel Nider5bd73f82011-12-14 16:53:30 +02001644 if (channel_id >= TSPP_NUM_CHANNELS) {
1645 pr_err("tspp: channel id out of range");
1646 return NULL;
1647 }
1648 pdev = tspp_find_by_id(dev);
1649 if (!pdev) {
1650 pr_err("tspp_get: can't find device %i", dev);
1651 return NULL;
1652 }
1653 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001654
Joel Nider5bd73f82011-12-14 16:53:30 +02001655 if (!channel->read) {
1656 pr_warn("tspp: no buffer to get on channel %i!",
1657 channel->id);
1658 return NULL;
1659 }
1660
1661 buffer = channel->read;
1662 /* see if we have any buffers ready to read */
1663 if (buffer->state != TSPP_BUF_STATE_DATA)
1664 return 0;
1665
1666 if (buffer->state == TSPP_BUF_STATE_DATA) {
1667 /* mark the buffer as busy */
1668 buffer->state = TSPP_BUF_STATE_LOCKED;
1669
1670 /* increment the pointer along the list */
1671 channel->read = channel->read->next;
1672 }
1673
1674 return &buffer->desc;
1675}
1676EXPORT_SYMBOL(tspp_get_buffer);
1677
1678int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
1679{
1680 int i, found = 0;
1681 struct tspp_mem_buffer *buffer;
1682 struct tspp_channel *channel;
1683 struct tspp_device *pdev;
1684
1685 if (channel_id >= TSPP_NUM_CHANNELS) {
1686 pr_err("tspp: channel id out of range");
1687 return -ECHRNG;
1688 }
1689 pdev = tspp_find_by_id(dev);
1690 if (!pdev) {
1691 pr_err("tspp: can't find device %i", dev);
1692 return -ENODEV;
1693 }
1694 channel = &pdev->channels[channel_id];
1695
1696 if (descriptor_id > channel->buffer_count)
1697 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
1698
1699 /* find the correct descriptor */
1700 buffer = channel->locked;
1701 for (i = 0; i < channel->buffer_count; i++) {
1702 if (buffer->desc.id == descriptor_id) {
1703 found = 1;
1704 break;
1705 }
1706 buffer = buffer->next;
1707 }
1708 channel->locked = channel->locked->next;
1709
1710 if (!found) {
1711 pr_err("tspp: cant find desc %i", descriptor_id);
1712 return -EINVAL;
1713 }
1714
1715 /* make sure the buffer is in the expected state */
1716 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
1717 pr_err("tspp: buffer %i not locked", descriptor_id);
1718 return -EINVAL;
1719 }
1720 /* unlock the buffer and requeue it */
1721 buffer->state = TSPP_BUF_STATE_WAITING;
1722
1723 if (tspp_queue_buffer(channel, buffer))
1724 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02001725 return 0;
1726}
Joel Nider5bd73f82011-12-14 16:53:30 +02001727EXPORT_SYMBOL(tspp_release_buffer);
1728
1729int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count,
1730 u32 size, u32 int_freq, tspp_allocator *alloc, void *user)
1731{
1732 struct tspp_channel *channel;
1733 struct tspp_device *pdev;
1734 struct tspp_mem_buffer *last = NULL;
1735
1736 TSPP_DEBUG("tspp_allocate_buffers");
1737
1738 if (channel_id >= TSPP_NUM_CHANNELS) {
1739 pr_err("tspp: channel id out of range");
1740 return -ECHRNG;
1741 }
1742 pdev = tspp_find_by_id(dev);
1743 if (!pdev) {
1744 pr_err("tspp_alloc: can't find device %i", dev);
1745 return -ENODEV;
1746 }
1747 channel = &pdev->channels[channel_id];
1748
1749 channel->max_buffers = count;
1750
1751 /* set up interrupt frequency */
1752 if (int_freq > channel->max_buffers)
1753 int_freq = channel->max_buffers;
1754 channel->int_freq = int_freq;
1755
1756 switch (channel->mode) {
1757 case TSPP_MODE_DISABLED:
1758 case TSPP_MODE_PES:
1759 /* give the user what he asks for */
1760 channel->buffer_size = size;
1761 break;
1762
1763 case TSPP_MODE_RAW:
1764 /* must be a multiple of 192 */
1765 if (size < (TSPP_PACKET_LENGTH+4))
1766 channel->buffer_size = (TSPP_PACKET_LENGTH+4);
1767 else
1768 channel->buffer_size = (size /
1769 (TSPP_PACKET_LENGTH+4)) *
1770 (TSPP_PACKET_LENGTH+4);
1771 break;
1772
1773 case TSPP_MODE_RAW_NO_SUFFIX:
1774 /* must be a multiple of 188 */
1775 channel->buffer_size = (size / TSPP_PACKET_LENGTH) *
1776 TSPP_PACKET_LENGTH;
1777 break;
1778 }
1779
1780 for (; channel->buffer_count < channel->max_buffers;
1781 channel->buffer_count++) {
1782
1783 /* allocate the descriptor */
1784 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
1785 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
1786 if (!desc) {
1787 pr_warn("tspp: Can't allocate desc %i",
1788 channel->buffer_count);
1789 break;
1790 }
1791
1792 desc->desc.id = channel->buffer_count;
1793 /* allocate the buffer */
1794 if (tspp_alloc_buffer(channel_id, &desc->desc,
1795 channel->buffer_size, alloc, user) != 0) {
1796 kfree(desc);
1797 pr_warn("tspp: Can't allocate buffer %i",
1798 channel->buffer_count);
1799 break;
1800 }
1801
1802 /* add the descriptor to the list */
1803 desc->filled = 0;
1804 desc->read_index = 0;
1805 if (!channel->data) {
1806 channel->data = desc;
1807 desc->next = channel->data;
1808 } else {
1809 last->next = desc;
1810 }
1811 last = desc;
1812 desc->next = channel->data;
1813
1814 /* prepare the sps descriptor */
1815 desc->sps.phys_base = desc->desc.phys_base;
1816 desc->sps.base = desc->desc.virt_base;
1817 desc->sps.size = desc->desc.size;
1818
1819 /* start the transfer */
1820 if (tspp_queue_buffer(channel, desc))
1821 pr_err("tspp: can't queue buffer %i", desc->desc.id);
1822 }
1823
1824 channel->waiting = channel->data;
1825 channel->read = channel->data;
1826 channel->locked = channel->data;
1827 return 0;
1828}
1829EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02001830
1831/*** File Operations ***/
1832static ssize_t tspp_open(struct inode *inode, struct file *filp)
1833{
Joel Nider5bd73f82011-12-14 16:53:30 +02001834 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02001835 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001836
1837 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02001838 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
1839 filp->private_data = channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001840 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02001841
1842 /* if this channel is already in use, quit */
1843 if (channel->used) {
1844 pr_err("tspp channel %i already in use",
1845 MINOR(channel->cdev.dev));
1846 return -EACCES;
1847 }
1848
Joel Nider5bd73f82011-12-14 16:53:30 +02001849 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02001850 pr_err("tspp: error opening channel");
1851 return -EACCES;
1852 }
1853
1854 return 0;
1855}
1856
1857static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
1858{
1859 unsigned long flags;
1860 unsigned int mask = 0;
1861 struct tspp_channel *channel;
1862 channel = filp->private_data;
1863
1864 /* register the wait queue for this channel */
1865 poll_wait(filp, &channel->in_queue, p);
1866
1867 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider5bd73f82011-12-14 16:53:30 +02001868 if (channel->read &&
1869 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02001870 mask = POLLIN | POLLRDNORM;
1871
1872 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
1873
1874 return mask;
1875}
1876
1877static ssize_t tspp_release(struct inode *inode, struct file *filp)
1878{
Joel Nider5bd73f82011-12-14 16:53:30 +02001879 struct tspp_channel *channel = filp->private_data;
1880 u32 dev = channel->pdev->pdev->id;
1881 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02001882
Joel Nider5bd73f82011-12-14 16:53:30 +02001883 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02001884
1885 return 0;
1886}
1887
1888static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
1889 loff_t *f_pos)
1890{
1891 size_t size = 0;
1892 size_t transferred = 0;
1893 struct tspp_channel *channel;
1894 struct tspp_mem_buffer *buffer;
1895 channel = filp->private_data;
1896
1897 TSPP_DEBUG("tspp_read");
Joel Nider5bd73f82011-12-14 16:53:30 +02001898
1899 while (!channel->read) {
1900 if (filp->f_flags & O_NONBLOCK) {
1901 pr_warn("tspp: no buffer on channel %i!",
1902 channel->id);
1903 return -EAGAIN;
1904 }
1905 /* go to sleep if there is nothing to read */
1906 if (wait_event_interruptible(channel->in_queue,
1907 (channel->read != NULL))) {
1908 pr_err("tspp: rude awakening\n");
1909 return -ERESTARTSYS;
1910 }
1911 }
1912
1913 buffer = channel->read;
1914
Joel Nider5556a852011-10-16 10:52:13 +02001915 /* see if we have any buffers ready to read */
1916 while (buffer->state != TSPP_BUF_STATE_DATA) {
1917 if (filp->f_flags & O_NONBLOCK) {
1918 pr_warn("tspp: nothing to read on channel %i!",
1919 channel->id);
1920 return -EAGAIN;
1921 }
1922 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02001923 if (wait_event_interruptible(channel->in_queue,
1924 (buffer->state == TSPP_BUF_STATE_DATA))) {
1925 pr_err("tspp: rude awakening\n");
1926 return -ERESTARTSYS;
1927 }
1928 }
1929
1930 while (buffer->state == TSPP_BUF_STATE_DATA) {
1931 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02001932 if (size == 0)
1933 break;
1934
Joel Nider5bd73f82011-12-14 16:53:30 +02001935 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02001936 buffer->read_index, size)) {
1937 pr_err("tspp: error copying to user buffer");
Joel Nider5bd73f82011-12-14 16:53:30 +02001938 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001939 }
1940 buf += size;
1941 count -= size;
1942 transferred += size;
1943 buffer->read_index += size;
1944
1945 /* after reading the end of the buffer, requeue it,
1946 and set up for reading the next one */
Joel Nider5bd73f82011-12-14 16:53:30 +02001947 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02001948 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5bd73f82011-12-14 16:53:30 +02001949 if (tspp_queue_buffer(channel, buffer))
1950 pr_err("tspp: can't submit transfer");
1951 channel->locked = channel->read;
1952 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02001953 }
1954 }
1955
1956 return transferred;
1957}
1958
1959static long tspp_ioctl(struct file *filp,
1960 unsigned int param0, unsigned long param1)
1961{
Joel Nider5bd73f82011-12-14 16:53:30 +02001962 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02001963 int rc = -1;
1964 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001965 struct tspp_select_source ss;
1966 struct tspp_filter f;
1967 struct tspp_key k;
1968 struct tspp_iv iv;
1969 struct tspp_system_keys sk;
1970 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02001971 channel = filp->private_data;
Joel Nider5bd73f82011-12-14 16:53:30 +02001972 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02001973
1974 if (!param1)
1975 return -EINVAL;
1976
1977 switch (param0) {
1978 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider5bd73f82011-12-14 16:53:30 +02001979 if (!access_ok(VERIFY_READ, param1,
1980 sizeof(struct tspp_select_source))) {
1981 return -EBUSY;
1982 }
1983 if (__copy_from_user(&ss, (void *)param1,
1984 sizeof(struct tspp_select_source)) == 0)
1985 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02001986 break;
1987 case TSPP_IOCTL_ADD_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02001988 if (!access_ok(VERIFY_READ, param1,
1989 sizeof(struct tspp_filter))) {
1990 return -ENOSR;
1991 }
1992 if (__copy_from_user(&f, (void *)param1,
1993 sizeof(struct tspp_filter)) == 0)
1994 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02001995 break;
1996 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02001997 if (!access_ok(VERIFY_READ, param1,
1998 sizeof(struct tspp_filter))) {
1999 return -EBUSY;
2000 }
2001 if (__copy_from_user(&f, (void *)param1,
2002 sizeof(struct tspp_filter)) == 0)
2003 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002004 break;
2005 case TSPP_IOCTL_SET_KEY:
Joel Nider5bd73f82011-12-14 16:53:30 +02002006 if (!access_ok(VERIFY_READ, param1,
2007 sizeof(struct tspp_key))) {
2008 return -EBUSY;
2009 }
2010 if (__copy_from_user(&k, (void *)param1,
2011 sizeof(struct tspp_key)) == 0)
2012 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002013 break;
2014 case TSPP_IOCTL_SET_IV:
Joel Nider5bd73f82011-12-14 16:53:30 +02002015 if (!access_ok(VERIFY_READ, param1,
2016 sizeof(struct tspp_iv))) {
2017 return -EBUSY;
2018 }
2019 if (__copy_from_user(&iv, (void *)param1,
2020 sizeof(struct tspp_iv)) == 0)
2021 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002022 break;
2023 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider5bd73f82011-12-14 16:53:30 +02002024 if (!access_ok(VERIFY_READ, param1,
2025 sizeof(struct tspp_system_keys))) {
2026 return -EINVAL;
2027 }
2028 if (__copy_from_user(&sk, (void *)param1,
2029 sizeof(struct tspp_system_keys)) == 0)
2030 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002031 break;
2032 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002033 if (!access_ok(VERIFY_READ, param1,
2034 sizeof(struct tspp_buffer))) {
2035 rc = -EINVAL;
2036 }
2037 if (__copy_from_user(&b, (void *)param1,
2038 sizeof(struct tspp_buffer)) == 0)
2039 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002040 break;
2041 default:
2042 pr_err("tspp: Unknown ioctl %i", param0);
2043 }
2044
2045 /* normalize the return code in case one of the subfunctions does
2046 something weird */
2047 if (rc != 0)
Joel Nider5bd73f82011-12-14 16:53:30 +02002048 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002049
2050 return rc;
2051}
2052
2053/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002054static int debugfs_iomem_x32_set(void *data, u64 val)
2055{
2056 writel_relaxed(val, data);
2057 wmb();
2058 return 0;
2059}
2060
2061static int debugfs_iomem_x32_get(void *data, u64 *val)
2062{
2063 *val = readl_relaxed(data);
2064 return 0;
2065}
2066
2067DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2068 debugfs_iomem_x32_set, "0x%08llx");
2069
2070static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2071 int instance)
2072{
2073 char name[10];
2074 snprintf(name, 10, "tsif%i", instance);
2075 tsif_device->dent_tsif = debugfs_create_dir(
2076 name, NULL);
2077 if (tsif_device->dent_tsif) {
2078 int i;
2079 void __iomem *base = tsif_device->base;
2080 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2081 tsif_device->debugfs_tsif_regs[i] =
2082 debugfs_create_file(
2083 debugfs_tsif_regs[i].name,
2084 debugfs_tsif_regs[i].mode,
2085 tsif_device->dent_tsif,
2086 base + debugfs_tsif_regs[i].offset,
2087 &fops_iomem_x32);
2088 }
2089 }
2090}
2091
2092static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2093{
2094 if (tsif_device->dent_tsif) {
2095 int i;
2096 debugfs_remove_recursive(tsif_device->dent_tsif);
2097 tsif_device->dent_tsif = NULL;
2098 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2099 tsif_device->debugfs_tsif_regs[i] = NULL;
2100 }
2101}
2102
2103static void tspp_debugfs_init(struct tspp_device *device, int instance)
2104{
2105 char name[10];
2106 snprintf(name, 10, "tspp%i", instance);
2107 device->dent = debugfs_create_dir(
2108 name, NULL);
2109 if (device->dent) {
2110 int i;
2111 void __iomem *base = device->base;
2112 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2113 device->debugfs_regs[i] =
2114 debugfs_create_file(
2115 debugfs_tspp_regs[i].name,
2116 debugfs_tspp_regs[i].mode,
2117 device->dent,
2118 base + debugfs_tspp_regs[i].offset,
2119 &fops_iomem_x32);
2120 }
2121 }
2122}
2123
2124static void tspp_debugfs_exit(struct tspp_device *device)
2125{
2126 if (device->dent) {
2127 int i;
2128 debugfs_remove_recursive(device->dent);
2129 device->dent = NULL;
2130 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2131 device->debugfs_regs[i] = NULL;
2132 }
2133}
Joel Nider5556a852011-10-16 10:52:13 +02002134
2135static int __devinit msm_tspp_probe(struct platform_device *pdev)
2136{
2137 int rc = -ENODEV;
2138 u32 version;
2139 u32 i;
2140 struct msm_tspp_platform_data *data;
2141 struct tspp_device *device;
2142 struct resource *mem_tsif0;
2143 struct resource *mem_tsif1;
2144 struct resource *mem_tspp;
2145 struct resource *mem_bam;
Joel Nider5556a852011-10-16 10:52:13 +02002146
2147 /* must have platform data */
2148 data = pdev->dev.platform_data;
2149 if (!data) {
2150 pr_err("tspp: Platform data not available");
2151 rc = -EINVAL;
2152 goto out;
2153 }
2154
2155 /* check for valid device id */
Joel Nider5bd73f82011-12-14 16:53:30 +02002156 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002157 pr_err("tspp: Invalid device ID %d", pdev->id);
2158 rc = -EINVAL;
2159 goto out;
2160 }
2161
2162 /* OK, we will use this device */
2163 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2164 if (!device) {
2165 pr_err("tspp: Failed to allocate memory for device");
2166 rc = -ENOMEM;
2167 goto out;
2168 }
2169
2170 /* set up references */
2171 device->pdev = pdev;
2172 platform_set_drvdata(pdev, device);
2173
2174 /* map clocks */
2175 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002176 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002177 if (IS_ERR(device->tsif_pclk)) {
2178 pr_err("tspp: failed to get %s",
2179 data->tsif_pclk);
2180 rc = PTR_ERR(device->tsif_pclk);
2181 device->tsif_pclk = NULL;
2182 goto err_pclock;
2183 }
2184 }
2185 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002186 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002187 if (IS_ERR(device->tsif_ref_clk)) {
2188 pr_err("tspp: failed to get %s",
2189 data->tsif_ref_clk);
2190 rc = PTR_ERR(device->tsif_ref_clk);
2191 device->tsif_ref_clk = NULL;
2192 goto err_refclock;
2193 }
2194 }
2195
2196 /* map I/O memory */
2197 mem_tsif0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2198 if (!mem_tsif0) {
2199 pr_err("tspp: Missing tsif0 MEM resource");
2200 rc = -ENXIO;
2201 goto err_res_tsif0;
2202 }
2203 device->tsif[0].base = ioremap(mem_tsif0->start,
2204 resource_size(mem_tsif0));
2205 if (!device->tsif[0].base) {
2206 pr_err("tspp: ioremap failed");
2207 goto err_map_tsif0;
2208 }
2209
2210 mem_tsif1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2211 if (!mem_tsif1) {
2212 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2213 rc = -ENXIO;
2214 goto err_res_tsif1;
2215 }
2216 device->tsif[1].base = ioremap(mem_tsif1->start,
2217 resource_size(mem_tsif1));
2218 if (!device->tsif[1].base) {
2219 dev_err(&pdev->dev, "ioremap failed");
2220 goto err_map_tsif1;
2221 }
2222
2223 mem_tspp = platform_get_resource(pdev, IORESOURCE_MEM, 2);
2224 if (!mem_tspp) {
2225 dev_err(&pdev->dev, "Missing MEM resource");
2226 rc = -ENXIO;
2227 goto err_res_dev;
2228 }
2229 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2230 if (!device->base) {
2231 dev_err(&pdev->dev, "ioremap failed");
2232 goto err_map_dev;
2233 }
2234
2235 mem_bam = platform_get_resource(pdev, IORESOURCE_MEM, 3);
2236 if (!mem_bam) {
2237 pr_err("tspp: Missing bam MEM resource");
2238 rc = -ENXIO;
2239 goto err_res_bam;
2240 }
2241 memset(&device->bam_props, 0, sizeof(device->bam_props));
2242 device->bam_props.phys_addr = mem_bam->start;
2243 device->bam_props.virt_addr = ioremap(mem_bam->start,
2244 resource_size(mem_bam));
2245 if (!device->bam_props.virt_addr) {
2246 dev_err(&pdev->dev, "ioremap failed");
2247 goto err_map_bam;
2248 }
2249
2250 /* map TSPP IRQ */
2251 rc = platform_get_irq(pdev, 0);
2252 if (rc > 0) {
2253 device->tspp_irq = rc;
2254 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2255 dev_name(&pdev->dev), device);
2256 if (rc) {
2257 dev_err(&pdev->dev, "failed to request IRQ %d : %d",
2258 device->tspp_irq, rc);
2259 goto err_irq;
2260 }
2261 } else {
2262 dev_err(&pdev->dev, "failed to get tspp IRQ");
2263 goto err_irq;
2264 }
2265
2266 /* BAM IRQ */
2267 device->bam_irq = TSIF_BAM_IRQ;
2268
2269 /* GPIOs */
2270 rc = tspp_start_gpios(device);
2271 if (rc)
2272 goto err_gpio;
2273
2274 /* power management */
2275 pm_runtime_set_active(&pdev->dev);
2276 pm_runtime_enable(&pdev->dev);
2277
Joel Nider5556a852011-10-16 10:52:13 +02002278 tspp_debugfs_init(device, 0);
2279
2280 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2281 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002282
2283 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2284 dev_name(&pdev->dev));
2285
2286 /* set up pointers to ram-based 'registers' */
Joel Nider5bd73f82011-12-14 16:53:30 +02002287 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2288 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2289 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2290 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2291 device->tspp_global_performance =
2292 device->base + TSPP_GLOBAL_PERFORMANCE;
2293 device->tspp_pipe_context =
2294 device->base + TSPP_PIPE_CONTEXT;
2295 device->tspp_pipe_performance =
2296 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002297
2298 device->bam_props.summing_threshold = 0x10;
2299 device->bam_props.irq = device->bam_irq;
2300 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2301
2302 if (sps_register_bam_device(&device->bam_props,
2303 &device->bam_handle) != 0) {
2304 pr_err("tspp: failed to register bam");
2305 goto err_bam;
2306 }
2307
Joel Nider5bd73f82011-12-14 16:53:30 +02002308 if (tspp_clock_start(device) != 0) {
2309 dev_err(&pdev->dev, "Can't start clocks");
2310 goto err_clock;
Joel Nider5556a852011-10-16 10:52:13 +02002311 }
2312
2313 spin_lock_init(&device->spinlock);
2314 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2315 (unsigned long)device);
2316
2317 /* initialize everything to a known state */
2318 tspp_global_reset(device);
2319
2320 version = readl_relaxed(device->base + TSPP_VERSION);
2321 if (version != 1)
2322 pr_warn("tspp: unrecognized hw version=%i", version);
2323
Joel Nider5bd73f82011-12-14 16:53:30 +02002324 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002325 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002326 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2327 pr_err("tspp_channel_init failed");
2328 goto err_channel;
2329 }
Joel Nider5556a852011-10-16 10:52:13 +02002330 }
2331
Joel Nider5bd73f82011-12-14 16:53:30 +02002332 /* stop the clocks for power savings */
2333 tspp_clock_stop(device);
2334
2335 /* everything is ok, so add the device to the list */
2336 list_add_tail(&(device->devlist), &tspp_devices);
2337
Joel Nider5556a852011-10-16 10:52:13 +02002338 return 0;
2339
Joel Nider5bd73f82011-12-14 16:53:30 +02002340err_channel:
2341err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02002342 sps_deregister_bam_device(device->bam_handle);
2343err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02002344 tspp_debugfs_exit(device);
2345 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2346 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002347err_gpio:
2348err_irq:
2349 tspp_stop_gpios(device);
2350 iounmap(device->bam_props.virt_addr);
2351err_map_bam:
2352err_res_bam:
2353 iounmap(device->base);
2354err_map_dev:
2355err_res_dev:
2356 iounmap(device->tsif[1].base);
2357err_map_tsif1:
2358err_res_tsif1:
2359 iounmap(device->tsif[0].base);
2360err_map_tsif0:
2361err_res_tsif0:
2362 if (device->tsif_ref_clk)
2363 clk_put(device->tsif_ref_clk);
2364err_refclock:
2365 if (device->tsif_pclk)
2366 clk_put(device->tsif_pclk);
2367err_pclock:
2368 kfree(device);
2369
2370out:
2371 return rc;
2372}
2373
2374static int __devexit msm_tspp_remove(struct platform_device *pdev)
2375{
Joel Nider5bd73f82011-12-14 16:53:30 +02002376 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002377 u32 i;
Joel Nider5556a852011-10-16 10:52:13 +02002378
2379 struct tspp_device *device = platform_get_drvdata(pdev);
2380
Joel Nider5bd73f82011-12-14 16:53:30 +02002381 /* free the buffers, and delete the channels */
2382 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
2383 channel = &device->channels[i];
2384 tspp_close_channel(device->pdev->id, i);
2385 device_destroy(tspp_class, channel->cdev.dev);
2386 cdev_del(&channel->cdev);
2387 }
2388
Joel Nider5556a852011-10-16 10:52:13 +02002389 sps_deregister_bam_device(device->bam_handle);
2390
Joel Nider5556a852011-10-16 10:52:13 +02002391 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2392 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002393
2394 wake_lock_destroy(&device->wake_lock);
2395 free_irq(device->tspp_irq, device);
2396 tspp_stop_gpios(device);
2397
2398 iounmap(device->bam_props.virt_addr);
2399 iounmap(device->base);
2400 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2401 iounmap(device->tsif[i].base);
2402
2403 if (device->tsif_ref_clk)
2404 clk_put(device->tsif_ref_clk);
2405
2406 if (device->tsif_pclk)
2407 clk_put(device->tsif_pclk);
2408
2409 pm_runtime_disable(&pdev->dev);
2410 pm_runtime_put(&pdev->dev);
2411 kfree(device);
2412
2413 return 0;
2414}
2415
2416/*** power management ***/
2417
2418static int tspp_runtime_suspend(struct device *dev)
2419{
2420 dev_dbg(dev, "pm_runtime: suspending...");
2421 return 0;
2422}
2423
2424static int tspp_runtime_resume(struct device *dev)
2425{
2426 dev_dbg(dev, "pm_runtime: resuming...");
2427 return 0;
2428}
2429
2430static const struct dev_pm_ops tspp_dev_pm_ops = {
2431 .runtime_suspend = tspp_runtime_suspend,
2432 .runtime_resume = tspp_runtime_resume,
2433};
2434
2435static struct platform_driver msm_tspp_driver = {
2436 .probe = msm_tspp_probe,
2437 .remove = __exit_p(msm_tspp_remove),
2438 .driver = {
2439 .name = "msm_tspp",
2440 .pm = &tspp_dev_pm_ops,
2441 },
2442};
2443
2444
2445static int __init mod_init(void)
2446{
Joel Nider5556a852011-10-16 10:52:13 +02002447 int rc;
2448
Joel Nider5bd73f82011-12-14 16:53:30 +02002449 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02002450 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
2451 if (rc) {
2452 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
2453 goto err_devrgn;
2454 }
2455
2456 tspp_class = class_create(THIS_MODULE, "tspp");
2457 if (IS_ERR(tspp_class)) {
2458 rc = PTR_ERR(tspp_class);
2459 pr_err("tspp: Error creating class: %d", rc);
2460 goto err_class;
2461 }
2462
Joel Nider5bd73f82011-12-14 16:53:30 +02002463 /* register the driver, and check hardware */
2464 rc = platform_driver_register(&msm_tspp_driver);
2465 if (rc) {
2466 pr_err("tspp: platform_driver_register failed: %d", rc);
2467 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02002468 }
2469
2470 return 0;
2471
Joel Nider5bd73f82011-12-14 16:53:30 +02002472err_register:
2473 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02002474err_class:
2475 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
2476err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02002477 return rc;
2478}
2479
2480static void __exit mod_exit(void)
2481{
Joel Nider5bd73f82011-12-14 16:53:30 +02002482 /* delete low level driver */
2483 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02002484
Joel Nider5bd73f82011-12-14 16:53:30 +02002485 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02002486 class_destroy(tspp_class);
2487 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02002488}
2489
2490module_init(mod_init);
2491module_exit(mod_exit);
2492
Joel Nider5bd73f82011-12-14 16:53:30 +02002493MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02002494MODULE_LICENSE("GPL v2");